-
Notifications
You must be signed in to change notification settings - Fork 0
Single Lock Queue
Implementation here.
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:
-
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.
-
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.
-
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
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/