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

Demonstration of Thread-Safe Data Access using Readers-Writers Lock. More...

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

Go to the source code of this file.

Classes

class  SharedMetaData
 Manages a shared resource with high-concurrency read access. More...
 

Functions

int main (void)
 Main execution logic.
 

Variables

std::mutex log_mtx
 Global mutex to prevent console output interleaving.
 

Detailed Description

Demonstration of Thread-Safe Data Access using Readers-Writers Lock.

  • This file illustrates the use of std::shared_mutex to allow multiple threads to read data simultaneously while ensuring that writing is an exclusive operation.

Definition in file ReaderWriterLock.cpp.

Function Documentation

◆ main()

int main ( void  )

Main execution logic.

  • Orchestrates a high-concurrency scenario in three phases:
  1. Initial writer followed by a burst of 20 readers.
  2. An interleaved writer that waits for the burst of readers to clear.
  3. Subsequent bursts of readers (30 total) that must wait for the writer to finish.

Definition at line 95 of file ReaderWriterLock.cpp.

95 {
96 SharedMetaData store;
97 std::vector<std::thread> threads;
98
99 // Phase 1 : Initial Write and High concurrency of readers
100 threads.emplace_back(&SharedMetaData::write_data, &store, 1, 99);
101 for(int i=0; i<=20; i++) {
102 threads.emplace_back(&SharedMetaData::read_data, &store, i);
103 }
104
105 // Phase 2 : Interleaving a writer
106 // This writer will wait until all active readers from Phase 1 finish their 200ms sleep.
107 std::this_thread::sleep_for(std::chrono::milliseconds(700));
108 threads.emplace_back(&SharedMetaData::write_data, &store, 2, 234);
109
110 // Phase 3 : Post-write readers
111 // These readers will block until Writer 2 releases the unique_lock.
112 for(int i=21; i<=30; i++) {
113 threads.emplace_back(&SharedMetaData::read_data, &store, i);
114 }
115
116 for(int i=31; i<=40; i++) {
117 threads.emplace_back(&SharedMetaData::read_data, &store, i);
118 }
119
120 for(int i=41; i<=50; i++) {
121 threads.emplace_back(&SharedMetaData::read_data, &store, i);
122 }
123
124 // Join all threads to ensure clean termination
125 for(auto& t : threads) {
126 if(t.joinable())
127 t.join();
128 }
129
130 return 0;
131}
Manages a shared resource with high-concurrency read access.
void read_data(int thread_id)
Reads the shared data concurrently.
void write_data(int thread_id, int new_data)
Updates the shared data exclusively.

References SharedMetaData::read_data(), and SharedMetaData::write_data().

Here is the call graph for this function:

Variable Documentation

◆ log_mtx

std::mutex log_mtx

Global mutex to prevent console output interleaving.

** Standard output (std::cout) is not inherently thread-safe for atomic operations. This mutex ensures that logs from different threads do not garble each other.

Definition at line 19 of file ReaderWriterLock.cpp.

Referenced by SharedMetaData::read_data(), and SharedMetaData::write_data().