Chapter 12. Threads: It’s a parallel world

Programs often need to do several things at the same time.
POSIX threads can make your code more responsive by spinning off several pieces of code to run in parallel. But be careful! Threads are powerful tools, but you don’t want them crashing into each other. In this chapter, you’ll learn how to put up traffic signs and lane markers that will prevent a code pileup. By the end, you will know how to create POSIX threads and how to use synchronization mechanisms to protect the integrity of sensitive data.
Tasks are sequential...or not...
Imagine you are writing something complex like a game in C. The code will need to perform several different tasks:

Not only will your code need to do all of these things, but it will need to do them all at the same time. That’s going to be true for many different programs. Chat programs will need to read text from the network and send data to the network at the same time. Media players will need to stream video to the display as well as watch for input from the user controls.
How can your code perform several different tasks at once?
...and processes are not always the answer
You’ve already learned how to make the computer do several things at once: with processes. In the last chapter, you built a network server that ...
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