How it works...
We will first define a mutex object by the name lock. Recall that a mutex is basically a lock associated with a shared resource. To read or modify the shared resource, a thread needs to first acquire the lock for that resource. We will define two variables of the pthread_t type , with the names tid1 and tid2, to store two thread identifiers.
We will invoke the pthread_mutex_init method that initializes the lock object with the default mutex attributes. When it's initialized, the lock object is in an unlocked state. We then invoke the pthread_create function twice to create two threads and assign their identifiers to the two variables tid1 and tid2, whose addresses are passed to the pthread_create function. The two threads ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access