Structural deformable models
Public Member Functions | Friends | List of all members
vuMutex Class Reference

#include <vuThread.h>

Public Member Functions

 vuMutex (bool recursive=false)
 
 ~vuMutex ()
 
void lock ()
 
bool trylock ()
 
void unlock ()
 

Friends

class vuLock
 

Detailed Description

Wrapper class for POSIX mutex. A mutex can be locked and unlocked. If a locked mutex is tried to be locked againg (e.g. by a parallel running thread) the thread is blocked until the mutex is free again. This can be used to manage access to shared resources or synchronize between different threads.

Definition at line 28 of file vuThread.h.

Constructor & Destructor Documentation

vuMutex::vuMutex ( bool  recursive = false)

Default constructor. Calls pthread_mutex_init()

Definition at line 117 of file vuThread.cpp.

References NULL.

Referenced by unlock().

118 {
119  mutex = (void*) new pthread_mutex_t;
120  if(recursive) {
121  pthread_mutexattr_t attr;
122  pthread_mutexattr_init(&attr);
123  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
124  // other: PTHREAD_MUTEX_FAST_NP (default), PTHREAD_MUTEX_ERRORCHECK_NP
125  pthread_mutex_init((pthread_mutex_t*)mutex, &attr);
126  pthread_mutexattr_destroy(&attr);
127  } else {
128  pthread_mutex_init((pthread_mutex_t*)mutex, NULL);
129  }
130 }
#define NULL
Definition: simpletypes.h:9
vuMutex::~vuMutex ( )

Destructor. Calls pthread_mutex_destroy()

Definition at line 132 of file vuThread.cpp.

Referenced by unlock().

133 {
134  pthread_mutex_destroy((pthread_mutex_t*) mutex);
135  delete (pthread_mutex_t*)mutex;
136 }

Member Function Documentation

void vuMutex::lock ( )

Locks the mutex. If the mutex has already been locked by another process this function waits until the mutex is free. This is the recommended method to synchronize between threads. While waiting for the mutex to get free no cpu time is waisted.

Definition at line 138 of file vuThread.cpp.

Referenced by Brain::run(), unlock(), and Brain::~Brain().

139 {
140  pthread_mutex_lock( (pthread_mutex_t*) mutex );
141 }
bool vuMutex::trylock ( )

Try to lock the mutex. Same as lock() but if mutex is busy the calling thread is not blocked.

Returns
true if mutex was successfully locked. false if mutex busy.

Definition at line 143 of file vuThread.cpp.

Referenced by unlock().

144 {
145  return pthread_mutex_trylock( (pthread_mutex_t*) mutex ) != EBUSY;
146 }
void vuMutex::unlock ( )

Unlock the mutex. Allow other threads to lock the mutex. If you forget this the program will get stuck...

Definition at line 148 of file vuThread.cpp.

References _kickoff(), lock(), NULL, vuThread::run(), vuThread::startThread(), vuThread::stopThread(), trylock(), unlock(), vuThread::usleep(), vuMutex(), and ~vuMutex().

Referenced by _kickoff(), Brain::run(), unlock(), and Brain::~Brain().

149 {
150  pthread_mutex_unlock( (pthread_mutex_t*) mutex );
151 }

Friends And Related Function Documentation

friend class vuLock
friend

Definition at line 30 of file vuThread.h.


The documentation for this class was generated from the following files: