首页 > 代码库 > pthread_wrap.h
pthread_wrap.h
#ifndef _PTHREAD_WRAP_H
#define _PTHREAD_WRAP_H
#include <pthread.h>
class hm_pthread_mutex
{
public:
hm_pthread_mutex()
{
pthread_mutex_init(&m_mutex, 0);
}
~hm_pthread_mutex()
{
pthread_mutex_destroy(&m_mutex);
}
inline void lock()
{
pthread_mutex_lock(&m_mutex);
}
inline void unlock()
{
pthread_mutex_unlock(&m_mutex);
}
private:
pthread_mutex_t m_mutex;
};
class hm_pthread_rwlock
{
public:
hm_pthread_rwlock()
{
pthread_rwlock_init(&m_rwlock, 0);
}
~hm_pthread_rwlock()
{
pthread_rwlock_destroy(&m_rwlock);
}
inline void rdlock()
{
pthread_rwlock_rdlock(&m_rwlock);
}
inline void wrlock()
{
pthread_rwlock_wrlock(&m_rwlock);
}
inline void unlock()
{
pthread_rwlock_unlock(&m_rwlock);
}
private:
pthread_rwlock_t m_rwlock;
};
class hm_pthread_cond
{
public:
hm_pthread_cond()
{
m_complete = false;
pthread_mutex_init(&m_mutex, 0);
pthread_cond_init(&m_cond, 0);
}
~hm_pthread_cond()
{
pthread_mutex_destroy(&m_mutex);
pthread_cond_destroy(&m_cond);
}
inline void wait()
{
pthread_mutex_lock(&m_mutex);
m_complete = false;
while (!m_complete)
pthread_cond_wait(&m_cond, &m_mutex);
pthread_mutex_unlock(&m_mutex);
}
inline void notify(bool complete=true)
{
pthread_mutex_lock(&m_mutex);
m_complete = complete;
pthread_cond_signal(&m_cond);
pthread_mutex_unlock(&m_mutex);
}
private:
bool m_complete;
pthread_mutex_t m_mutex;
pthread_cond_t m_cond;
};
#endif//_PTHREAD_WRAP_H
pthread_wrap.h