multithreading - iOS using pthread_mutexattr_t trouble -


i porting existing c++ project objective-c++ , came across mutex stuff. not sure done here neither if correct. initialize kind of multithreading lock mechanism (called "criticalsection") following done:

#include <pthread.h>  pthread_mutex_t cs; pthread_mutexattr_t attr; 

later in code:

pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, pthread_mutex_recursive); pthread_mutex_init(&cs, &attr); 

to enter "lock" there is:

pthread_mutex_lock(&cs); 

to leave "lock":

pthread_mutex_unlock(&cs); pthread_mutex_destroy(&cs); 

my question (since have no clue how done) is: correct implementation? because encounter problems lock mechanism not work (bad memory access errors, corrupt pointers in situations "criticalsection" used).

it's correct except don't want destroy mutex when unlocking. destroy mutex when can guarantee threads have finished it.

for example:

class criticalsection { public:     criticalsection()     {         pthread_mutexattr_t attr;         pthread_mutexattr_init(&attr);         pthread_mutexattr_settype(&attr, pthread_mutex_recursive);         pthread_mutex_init(&mutex, &attr);     }     ~criticalsection() { pthread_mutex_destroy(&mutex); }     void enter() { pthread_mutex_lock(&mutex); }     void leave() { pthread_mutex_unlock(&mutex); } private:     pthread_mutex_t mutex; }; 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -