POSIX Threads

Learn how parallel programming is a complex affair and get introduced to the POSIX thread library as the first of many tools we want to talk about.

Multithreading

The threads model of parallel programming is one in which a single process (a single program) can spawn multiple, concurrent “threads” (sub-programs). Each thread runs independently of the others, although they can all access the same shared memory space (and hence they can communicate with each other if necessary). Threads can be spawned and killed as required, by the main program.

Once spawned by a program, threads are concurrent units of processing that can then be delegated by the operating system to multiple processing cores. Clearly the advantage of a multithreaded program (one that uses multiple threads that are assigned to multiple processing cores) is that you can achieve big speedups, as all cores of your CPU (and all CPUs if you have more than one) are used at the same time.

Race conditions and synchronization

A challenge of using threads is the issue of collisions and race conditions. A race condition is a situation that arises when multiple threads are trying to access the same resource without proper coordination, which can result in errors and unintended consequences. For example, if multiple threads write to (and depend upon) a shared memory variable, then care must be taken to make sure that multiple threads don’t try to write to the same location simultaneously. There are mechanisms to synchronize threads, and to implement mutually exclusive access to shared variables so that the shared variables can be locked by one thread and then released, preventing collisions by other threads. These mechanisms ensure threads must “take turns” when accessing protected data.

We now want to go over different tools for implementing parallel programming starting with POSIX threads.

POSIX Threads (pthreads)

POSIX Threads (pthreads for short) is a standard for programming with threads, and defines a set of C types, functions and constants.

Here is a simple example program that spawns 5 threads, where each one runs the myFun function.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy