I have multiple threads accessing shared data. I want to put a lock on that code so that the clients access it one at a time. How should i do this?
Printable View
I have multiple threads accessing shared data. I want to put a lock on that code so that the clients access it one at a time. How should i do this?
There're many resources out there.Code:pthread_mutex_t lock;
int shared_data;
pthread_mutex_init(&lock,NULL);
....
pthread_mutex_lock(&lock);
// use shared_data
pthread_mutex_unlock(&lock);
Multithreaded Programming (POSIX pthreads Tutorial)