Reader-Writer problem using Monitors (pthreads) Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Prerequisite - Monitors, Readers-Writers Problem There is a shared resource that is accessed by multiple processes i.e. readers and writers. Any number of readers can read from the shared resource simultaneously, but only one writer can write to the shared resource at a time. When a writer is writing data to the resource, no other process can access the resource. A writer cannot write to the resource if there are any readers accessing the resource at that time. Similarly, a reader can not read if there is a writer accessing the resource or if there are any waiting writers.The Reader-Writer problem using a monitor can be implemented using pthreads. The POSIX threads (or pthread) libraries are a standards-based thread API for C/C++. The library provides the following synchronization mechanisms: Mutexes (pthread_mutex_t) - Mutual exclusion lock: Block access to variables by other threads. This enforces exclusive access by a thread to a variable or set of variables. Condition Variables - (pthread_cond_t): The condition variable mechanism allows threads to suspend execution and relinquish the processor until some condition is true. Implementation of Reader-Writer solution using pthread library:Execute the program using the following command in your Linux system $g++ -pthread program_name.cpp $./a.out or $g++ -pthread program_name.cpp -o object_name $./object_name Code: CPP // Reader-Writer problem using monitors #include <iostream> #include <pthread.h> #include <unistd.h> using namespace std; class monitor { private: // no. of readers int rcnt; // no. of writers int wcnt; // no. of readers waiting int waitr; // no. of writers waiting int waitw; // condition variable to check whether reader can read pthread_cond_t canread; // condition variable to check whether writer can write pthread_cond_t canwrite; // mutex for synchronization pthread_mutex_t condlock; public: monitor() { rcnt = 0; wcnt = 0; waitr = 0; waitw = 0; pthread_cond_init(&canread, NULL); pthread_cond_init(&canwrite, NULL); pthread_mutex_init(&condlock, NULL); } // mutex provide synchronization so that no other thread // can change the value of data void beginread(int i) { pthread_mutex_lock(&condlock); // if there are active or waiting writers if (wcnt == 1 || waitw > 0) { // incrementing waiting readers waitr++; // reader suspended pthread_cond_wait(&canread, &condlock); waitr--; } // else reader reads the resource rcnt++; cout << "reader " << i << " is reading\n"; pthread_mutex_unlock(&condlock); pthread_cond_broadcast(&canread); } void endread(int i) { // if there are no readers left then writer enters monitor pthread_mutex_lock(&condlock); if (--rcnt == 0) pthread_cond_signal(&canwrite); pthread_mutex_unlock(&condlock); } void beginwrite(int i) { pthread_mutex_lock(&condlock); // a writer can enter when there are no active // or waiting readers or other writer if (wcnt == 1 || rcnt > 0) { ++waitw; pthread_cond_wait(&canwrite, &condlock); --waitw; } wcnt = 1; cout << "writer " << i << " is writing\n"; pthread_mutex_unlock(&condlock); } void endwrite(int i) { pthread_mutex_lock(&condlock); wcnt = 0; // if any readers are waiting, threads are unblocked if (waitr > 0) pthread_cond_signal(&canread); else pthread_cond_signal(&canwrite); pthread_mutex_unlock(&condlock); } } // global object of monitor class M; void* reader(void* id) { int c = 0; int i = *(int*)id; // each reader attempts to read 5 times while (c < 5) { usleep(1); M.beginread(i); M.endread(i); c++; } } void* writer(void* id) { int c = 0; int i = *(int*)id; // each writer attempts to write 5 times while (c < 5) { usleep(1); M.beginwrite(i); M.endwrite(i); c++; } } int main() { pthread_t r[5], w[5]; int id[5]; for (int i = 0; i < 5; i++) { id[i] = i; // creating threads which execute reader function pthread_create(&r[i], NULL, &reader, &id[i]); // creating threads which execute writer function pthread_create(&w[i], NULL, &writer, &id[i]); } for (int i = 0; i < 5; i++) { pthread_join(r[i], NULL); } for (int i = 0; i < 5; i++) { pthread_join(w[i], NULL); } } Output: Comment More infoAdvertise with us Next Article Producer Consumer Problem using Semaphores | Set 1 P PrachiHooda Follow Improve Article Tags : Operating Systems Process Synchronization Similar Reads Readers-Writers Problem | Set 1 (Introduction and Readers Preference Solution) The readers-writer problem in operating systems is about managing access to shared data. It allows multiple readers to read data at the same time without issues but ensures that only one writer can write at a time, and no one can read while writing is happening. This helps prevent data corruption an 7 min read Readers-Writers Problem | Writers Preference Solution The Readers-Writers Problem is a classic synchronization challenge in operating systems and concurrent programming. It describes a scenario where multiple threads or processes need to access a shared resource (like a database or file) simultaneously. Some of these threads are "readers" (they only re 6 min read Producer Consumer Problem using Semaphores | Set 1 The Producer-Consumer problem is a classic synchronization issue in operating systems. It involves two types of processes: producers, which generate data, and consumers, which process that data. Both share a common buffer. The challenge is to ensure that the producer doesn't add data to a full buffe 4 min read Producer Consumer Problem using Semaphores | Set 1 The Producer-Consumer problem is a classic synchronization issue in operating systems. It involves two types of processes: producers, which generate data, and consumers, which process that data. Both share a common buffer. The challenge is to ensure that the producer doesn't add data to a full buffe 4 min read Monitors in Process Synchronization Monitors are a higher-level synchronization construct that simplifies process synchronization by providing a high-level abstraction for data access and synchronization. Monitors are implemented as programming language constructs, typically in object-oriented languages, and provide mutual exclusion, 3 min read Monitor vs Semaphore Both semaphores and monitors are used to solve the critical section problem (as they allow processes to access the shared resources in mutual exclusion) and to achieve process synchronization in the multiprocessing environment. Monitor: A Monitor type high-level synchronization construct. It is an a 2 min read Like