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 sectionsAs 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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.