4k views
Basic and easy pthread_mutex_lock example (C thread synchronization)
This is an extremely basic example of the usage of the pthread_mutex_lock function.
C
- #include <pthread.h>
- // This variable should be visible to all the threads
- pthread_mutex_t mutex;
- int main (int argc, const char * argv[])
- {
- pthread_mutex_init(&mutex, NULL);
- [...]
- pthread_mutex_destroy(&mutex);
- }
- void lockedFunction()
- {
- pthread_mutex_lock(&mutex);
- // ... do whatever you need here ...
- pthread_mutex_unlock(&mutex);
- }
Submitted by miguelSantirso over 2 years ago — last modified less than a minute ago