vcglib/wrap/system/multithreading/mutex.h

57 lines
882 B
C
Raw Normal View History

2011-12-09 17:06:08 +01:00
#ifndef MT_MUTEX_H
#define MT_MUTEX_H
#include "base.h"
#include <pthread.h>
namespace mt
{
class condition;
class mutex
{
2014-05-07 11:04:34 +02:00
MT_PREVENT_COPY(mutex)
2011-12-09 17:06:08 +01:00
2014-05-07 11:04:34 +02:00
public:
2011-12-09 17:06:08 +01:00
2014-05-07 11:04:34 +02:00
typedef mutex this_type;
typedef void base_type;
2011-12-09 17:06:08 +01:00
2014-05-07 11:04:34 +02:00
mutex(void)
{
pthread_mutex_init(&(this->m), 0);
}
2011-12-09 17:06:08 +01:00
2014-05-07 11:04:34 +02:00
~mutex(void)
{
pthread_mutex_destroy(&(this->m));
}
2011-12-09 17:06:08 +01:00
2014-05-07 11:04:34 +02:00
void lock(void)
{
pthread_mutex_lock(&(this->m));
}
2011-12-09 17:06:08 +01:00
2014-05-07 11:04:34 +02:00
void unlock(void)
{
pthread_mutex_unlock(&(this->m));
}
bool tryLock(void)
{
int a = pthread_mutex_trylock(&(this->m));
return a == 0;
}
2011-12-09 17:06:08 +01:00
2014-05-07 11:04:34 +02:00
private:
2011-12-09 17:06:08 +01:00
2014-05-07 11:04:34 +02:00
friend class condition;
2011-12-09 17:06:08 +01:00
2014-05-07 11:04:34 +02:00
pthread_mutex_t m;
2011-12-09 17:06:08 +01:00
};
}
#endif // MT_MUTEX_H