30.11.2010 - initial commit
This commit is contained in:
37
pimutex.h
Normal file
37
pimutex.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef PIMUTEX_H
|
||||
#define PIMUTEX_H
|
||||
|
||||
#include <pthread.h>
|
||||
#include "piincludes.h"
|
||||
|
||||
class PIMutex
|
||||
{
|
||||
public:
|
||||
PIMutex() {
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_PROCESS_SHARED);
|
||||
pthread_mutex_init(&mutex, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
}
|
||||
~PIMutex() {pthread_mutex_destroy(&mutex);}
|
||||
void lock() {pthread_mutex_lock(&mutex);}
|
||||
void unlock() {pthread_mutex_unlock(&mutex);}
|
||||
bool tryLock() {return (pthread_mutex_trylock(&mutex) == 0);}
|
||||
|
||||
private:
|
||||
pthread_mutex_t mutex;
|
||||
|
||||
};
|
||||
|
||||
class PIMutexLocker
|
||||
{
|
||||
public:
|
||||
PIMutexLocker(PIMutex * m): mutex(m) {mutex->lock();}
|
||||
PIMutexLocker(PIMutex & m): mutex(&m) {mutex->lock();}
|
||||
~PIMutexLocker() {mutex->unlock();}
|
||||
private:
|
||||
PIMutex * mutex;
|
||||
};
|
||||
|
||||
#endif // PIMUTEX_H
|
||||
Reference in New Issue
Block a user