1个回答
2013-06-15
展开全部
class Mutex
{
protected:
void *mutex_handler;
public:
// Constructure
Mutex()
{
mutex_handler = new pthread_mutex_t;
pthread_mutex_init((pthread_mutex_t *)mutex_handler, 0);
}
// DeConstructure
inline virtual ~Mutex()
{
if (mutex_handler)
{
pthread_mutex_destroy((pthread_mutex_t *)mutex_handler);
delete (pthread_mutex_t*)mutex_handler;
}
}
void MutexLock(void)
{
pthread_mutex_lock((pthread_mutex_t *)mutex_handler);
}
void MutexUnLock(void)
{
pthread_mutex_unlock((pthread_mutex_t *)mutex_handler);
}
};
// gobal interface for Mutex
struct Guard : public Mutex
{
explicit Guard(void *Handler)
{
Mutex::mutex_handler = Handler;
MutexLock();
}
inline virtual ~Guard()
{
MutexUnLock();
}
};
{
protected:
void *mutex_handler;
public:
// Constructure
Mutex()
{
mutex_handler = new pthread_mutex_t;
pthread_mutex_init((pthread_mutex_t *)mutex_handler, 0);
}
// DeConstructure
inline virtual ~Mutex()
{
if (mutex_handler)
{
pthread_mutex_destroy((pthread_mutex_t *)mutex_handler);
delete (pthread_mutex_t*)mutex_handler;
}
}
void MutexLock(void)
{
pthread_mutex_lock((pthread_mutex_t *)mutex_handler);
}
void MutexUnLock(void)
{
pthread_mutex_unlock((pthread_mutex_t *)mutex_handler);
}
};
// gobal interface for Mutex
struct Guard : public Mutex
{
explicit Guard(void *Handler)
{
Mutex::mutex_handler = Handler;
MutexLock();
}
inline virtual ~Guard()
{
MutexUnLock();
}
};
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询