Locks

In this lesson, we will learn about different locks and their usage in embedded programming.

Locks are available in three different forms:

  1. std::lock_guard for simple use-cases

  2. std::unique-lock for advanced use-cases

  3. std::shared_lock is available with C++14 and can be used to implement reader-writer locks.

Locks need the header <mutex>.

RAII-Idiom (Resource Acquisition Is Initialization)

Locks take care of their resource following the RAII idiom. A lock automatically binds its mutex in the constructor and releases it in the destructor. This reduces the risk of a deadlock because the runtime handles the mutex.

If the lock goes out of scope, the resource will be immediately released.

Get hands-on with 1000+ tech skills courses.