39 lines
850 B
C++
39 lines
850 B
C++
#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_mutexattr_setpshared(&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
|