#include <iostream>
#include <thread>
#include <shared_mutex>
//#include <unique_lock>
using namespace std;
int sharedresource=0;
shared_mutex shmtx;
void reader(int id)
{
shared_lock<shared_mutex> lock(shmtx);
cout<<"The variable is "<<sharedresource<<" of id"<<id<<endl;
}
void writer(int id)
{
unique_lock<shared_mutex> lock(shmtx);
sharedresource++;
cout<<"The id is "<<id<<endl;
}
int main() {
thread twriter[] = {thread(writer,1),thread(writer,2),thread(writer,3)};
thread treader[] = {thread(reader,1),thread(reader,2),thread(reader,3),thread(reader,4)};
for(auto t1:twriter) t1.join();
for(auto t2:treader) t2.join();
std::cout << "Hello world!" << std::endl;
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: