|
C++ Concurrency Sandbox
|
Manual implementation of a Readers-Writers Lock with Writer Preference. More...
#include <iostream>#include <thread>#include <mutex>#include <condition_variable>#include <vector>#include <chrono>Go to the source code of this file.
Classes | |
| class | RWLock |
| A custom synchronization primitive for the Readers-Writers problem. More... | |
Functions | |
| void | readerTask (RWLock *rw, int id) |
| Simulates a reader's lifecycle. | |
| void | writerTask (RWLock *rw, int id) |
| Simulates a writer's lifecycle. | |
| int | main (void) |
| Entry point for the custom RWLock demonstration. Creates a mix of reader and writer threads to demonstrate starvation protection. | |
Variables | |
| std::mutex | log_mtx |
| Global mutex to prevent console output interleaving. Standard output is not thread-safe; this ensures log messages remain legible. | |
Manual implementation of a Readers-Writers Lock with Writer Preference.
Definition in file CustomRWLock.cpp.
| int main | ( | void | ) |
Entry point for the custom RWLock demonstration. Creates a mix of reader and writer threads to demonstrate starvation protection.
Definition at line 144 of file CustomRWLock.cpp.
References readerTask(), and writerTask().
| void readerTask | ( | RWLock * | rw, |
| int | id | ||
| ) |
Simulates a reader's lifecycle.
| rw | Pointer to the shared RWLock instance. |
| id | Unique identifier for the reader thread. |
Definition at line 101 of file CustomRWLock.cpp.
References RWLock::lock_read(), log_mtx, and RWLock::unlock_read().
Referenced by main().
| void writerTask | ( | RWLock * | rw, |
| int | id | ||
| ) |
Simulates a writer's lifecycle.
| rw | Pointer to the shared RWLock instance. |
| id | Unique identifier for the writer thread. |
Definition at line 122 of file CustomRWLock.cpp.
References RWLock::lock_write(), log_mtx, and RWLock::unlock_write().
Referenced by main().
| std::mutex log_mtx |
Global mutex to prevent console output interleaving. Standard output is not thread-safe; this ensures log messages remain legible.
Definition at line 19 of file CustomRWLock.cpp.
Referenced by readerTask(), and writerTask().