1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#ifndef MT_MUTEX_H
#define MT_MUTEX_H
#include "base.h"
#include <pthread.h>
namespace mt
{
class condition;
class mutex
{
MT_PREVENT_COPY(mutex)
public:
typedef mutex this_type;
typedef void base_type;
mutex(void)
{
pthread_mutex_init(&(this->m), 0);
}
~mutex(void)
{
pthread_mutex_destroy(&(this->m));
}
void lock(void)
{
pthread_mutex_lock(&(this->m));
}
void unlock(void)
{
pthread_mutex_unlock(&(this->m));
}
bool tryLock(void)
{
int a = pthread_mutex_trylock(&(this->m));
return a == 0;
}
private:
friend class condition;
pthread_mutex_t m;
};
}
#endif // MT_MUTEX_H
|