C++ Concurrency Sandbox
Loading...
Searching...
No Matches
CustomRWLock.cpp File Reference

Manual implementation of a Readers-Writers Lock with Writer Preference. More...

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <vector>
#include <chrono>
Include dependency graph for CustomRWLock.cpp:

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.
 

Detailed Description

Manual implementation of a Readers-Writers Lock with Writer Preference.

  • Unlike std::shared_mutex, this implementation explicitly tracks waiting writers to prevent "Reader Starvation," ensuring writers aren't blocked forever by a continuous stream of concurrent readers.

Definition in file CustomRWLock.cpp.

Function Documentation

◆ main()

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.

144 {
145 RWLock rw;
146 std::vector<std::thread> threads;
147
148 for(int i=1; i<=50; i++) {
149 threads.emplace_back(readerTask, &rw, i);
150 }
151
152 std::this_thread::sleep_for(std::chrono::milliseconds(2000));
153
154 for(int i=1; i<=2; i++) {
155 threads.emplace_back(writerTask, &rw, i);
156 }
157
158 std::this_thread::sleep_for(std::chrono::milliseconds(2000));
159
160 for(int i=1001; i<=1100; i++) {
161 threads.emplace_back(readerTask, &rw, i);
162 }
163
164 for(auto& t : threads) {
165 if(t.joinable())
166 t.join();
167 }
168
169 std::cout << "All metadata operations completed." << std::endl;
170 return 0;
171}
void readerTask(RWLock *rw, int id)
Simulates a reader's lifecycle.
void writerTask(RWLock *rw, int id)
Simulates a writer's lifecycle.
A custom synchronization primitive for the Readers-Writers problem.

References readerTask(), and writerTask().

Here is the call graph for this function:

◆ readerTask()

void readerTask ( RWLock rw,
int  id 
)

Simulates a reader's lifecycle.

Parameters
rwPointer to the shared RWLock instance.
idUnique identifier for the reader thread.

Definition at line 101 of file CustomRWLock.cpp.

101 {
102 rw->lock_read();
103 {
104 std::lock_guard<std::mutex> log_lock(log_mtx);
105 std::cout << "[Reader " << id << "] Start Reading..." << std::endl;
106 }
107
108 std::this_thread::sleep_for(std::chrono::milliseconds(200));
109
110 {
111 std::lock_guard<std::mutex> log_lock(log_mtx);
112 std::cout << "[Reader " << id << "] Finished Reading." << std::endl;
113 }
114 rw->unlock_read();
115}
std::mutex log_mtx
Global mutex to prevent console output interleaving. Standard output is not thread-safe; this ensures...
void unlock_read()
Releases the shared read lock.
void lock_read()
Acquires a shared read lock.

References RWLock::lock_read(), log_mtx, and RWLock::unlock_read().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writerTask()

void writerTask ( RWLock rw,
int  id 
)

Simulates a writer's lifecycle.

Parameters
rwPointer to the shared RWLock instance.
idUnique identifier for the writer thread.

Definition at line 122 of file CustomRWLock.cpp.

122 {
123 std::this_thread::sleep_for(std::chrono::milliseconds(50));
124
125 rw->lock_write();
126 {
127 std::lock_guard<std::mutex> log_lock(log_mtx);
128 std::cout << ">>> [Writer " << id << "] EXCLUSIVE WRITE START <<<" << std::endl;
129 }
130
131 std::this_thread::sleep_for(std::chrono::milliseconds(500));
132
133 {
134 std::lock_guard<std::mutex> log_lock(log_mtx);
135 std::cout << ">>> [Writer " << id << "] EXCLUSIVE WRITE END <<<" << std::endl;
136 }
137 rw->unlock_write();
138}
void lock_write()
Acquires an exclusive write lock.
void unlock_write()
Releases the exclusive write lock.

References RWLock::lock_write(), log_mtx, and RWLock::unlock_write().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ log_mtx

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().