Thread safety via mutex locks

Given that a function does use global and/or static variables, and the decision is to continue to use them (the second approach we mention in Table 1), obviously the places in the code where they are used constitute critical sections. As Chapter 15, Multithreading with Pthreads Part II - Synchronization, has shown in detail, we must protect these critical sections; here, we use the pthreads mutex lock to do so.

For readability, only key parts of the source code are displayed here; to view the complete source code, build and run it, the entire tree is available for cloning from GitHub: https://github.com/PacktPublishing/Hands-on-System-Programming-with-Linux.

We apply this approach the addition of a pthread ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.