Skip to content

Single Lock Queue

Alina Bondarets edited this page Aug 28, 2022 · 11 revisions

Implementation here.

Overview

It is a simple concurrent First-In-First-Out (FIFO) queue using one mutex. It is based on a singly linked list with the support of blocking pops (try_pop).

The queue uses:

  1. Atomic variable to store the size of the queue. It wraps the counter to make it a thread-safe instance variable to be non-simultaneously used by different threads.

  2. Mutex as an instance variable. When one thread acquires it, no other thread can acquire it until it is released by that particular thread.

    • Lock-guard is a mutex wrapper used for owning the mutex. When a lock_guard object is created, it attempts to take ownership of the mutex it is given. When control leaves the scope in which the lock_guard object was created, the lock_guard is destructed, and the mutex is released.
  3. Condition variable is an event used for signaling between multiple threads. One or more threads can wait for a signal from another one.

    • It requires the unique_lock mutex wrapper, which allows using all the mutex methods

Useful links

https://en.cppreference.com/w/cpp/thread/lock_guard

https://en.cppreference.com/w/cpp/thread/unique_lock

https://en.cppreference.com/w/cpp/thread/condition_variable

https://thispointer.com/c11-multithreading-part-7-condition-variables-explained/

https://habr.com/ru/post/517918/

Clone this wiki locally