This recipe will teach you how to use POSIX mutexes to synchronize access to a resource from multiple threads. We'll do this by developing a program that contains a method (the critical section) that will perform a task that cannot run concurrently. We'll use the pthread_mutex_lock, pthread_mutex_unlock, and pthread_mutex_init POSIX methods to synchronize the threads' access to it.
Using POSIX mutexes
How to do it...
In this recipe, we'll create a multi-threaded program just to increment an integer to 200000. To do this, we'll develop the critical section that's responsible for incrementing the counter, which must be protected. Then, we'll develop the main section...