码迷,mamicode.com
首页 > 其他好文 > 详细

pthread_wrap.h

时间:2014-10-20 11:29:25      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:des   on   amp   ad   ef   line   as   class   public   

#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

标签:des   on   amp   ad   ef   line   as   class   public   

原文地址:http://www.cnblogs.com/walker-lc/p/4036674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!