In this lesson, we will learn about synchronization which ensures that threads in a multi-threaded program use shared resources safely.

Recall that the power of multi-threading comes from the ability to take one big task and split it up into a number of smaller tasks that can be executed concurrently. Using multi-threading, a five-second task split into five parts may only take one second to complete. With most multi-threaded programs, though, our threads will need to share some resources.

Sharing, in this context, means that each thread should at some point gain access to the resource so that it may do its work, but that only one thread may have access to it at a time.

Programmatically, these resources are used in various places in our code. These areas are known as critical sections. In order to synchronize our programs, we must ensure all critical sections have the following three properties:

  1. Mutual exclusion: only one thread can be inside the critical section at a time
  2. Progress: if no thread is inside the critical section, then a thread trying to access it must be allowed to do so
  3. Bounded waiting: each thread waiting to access the critical section must, at some point, gain access

If we use our synchronization tools to make sure these conditions are met for every critical section, then we have successfully synchronized our program.


Click Next when you’re ready to move to the next exercise.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?