#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
#include <csignal>
namespace
{
volatile std::sig_atomic_t gSignalStatus = 1;
}
void sig_handler(int sig){
gSignalStatus = 0;
}
boost::shared_mutex g_mutex;
using namespace std;
void reader(int id)
{
cerr<<"reader"<<id<<"started"<<endl;
while(gSignalStatus) {
boost::shared_lock<boost::shared_mutex> lock(g_mutex);
cerr << "reader"<<id << ": Got the lock" << endl;
boost::this_thread::sleep(boost::posix_time::milliseconds(200));
}
}
void writer(int id)
{
cerr<<"writer"<<id<<"started"<<endl;
while(gSignalStatus) {
boost::upgrade_lock<boost::shared_mutex> lock(g_mutex);
boost::upgrade_to_unique_lock<boost::shared_mutex> unique_lock(lock);
cout <<"writer"<< id << ": Got the lock" << endl;
boost::this_thread::sleep(boost::posix_time::milliseconds(200));
}
}
int main(int argc, char* argv[])
{
std::signal(SIGINT, sig_handler);
std::vector<boost::thread*> writerthread(1);
std::vector<boost::thread*> readerthread(4);
int id = 0;
for(auto& w:writerthread) w = new boost::thread(writer, id++);
id=0;
for(auto& r:readerthread) r = new boost::thread(reader, id++);
for(auto& w:writerthread){
w->join();
delete w;
}
for(auto&r:readerthread){
r->join();
delete r;
}
return 0;
}
I implemented multi readers/ single writer example.
The problem is once writer owns mutex or reader(s) own mutex, the owernership is not transferred to its opposite thread(readers->writer / writer->readers)
So the output of the program can be one of two.
When writer got the lock
writer0started
readerwriterreader0: Got the lock
readerreader21started30started
started
started
writer0: Got the lock
writer0: Got the lock
writer0: Got the lock
writer0: Got the lock
writer0: Got the lock
When reader(s) got the lock
writerreader0started
reader3startedreader
0: Got the lock
0reader2reader3: Got the lock
reader1started
reader1: Got the lock
started
started
reader2: Got the lock
reader0: Got the lock
reader3: Got the lock
reader1: Got the lock
reader2: Got the lock
reader1: Got the lock
reader2: Got the lock
reader0: Got the lock
reader3: Got the lock
readerreader3: Got the lock
reader2: Got the lock
0: Got the lock
The output was different than I expected.
What I expected was writer and reader(s) own the lock alternately.
Is this behavior is normal?
Is there any preference of locking mechanism? i.e. shared_lock
is preferred than upgrade_lock
.