#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;
}
我实现了多个读者/单个作家的例子。
问题是,一旦作家拥有互斥或读者拥有的互斥锁,权力就不会转移到其相反的线程(读者 - >作者/作者 - &gt;读者)
所以程序的输出可以是两个中的一个。
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
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
输出与我预期的不同。
我的期望是作家和读者交替拥有锁。
这种行为是否正常?
是否有锁定机制的偏好?即shared_lock
优先于upgrade_lock
。
答案 0 :(得分:2)
问题在于你有一个紧密的循环,一旦另一个人抓住互斥锁,读者或作者就不能轻易克服。看看你的循环要点:
第3步之后的窗口是读者或作者获取互斥锁的唯一机会。这个窗口非常短,所以实际抓住它的机会很小。这就是为什么你只看到作家或只有读者打印到控制台。实际上,如果你永远等待,你很可能会看到不同的实体将有机会工作。
那么如何解决呢?这很简单:只需将睡眠移出锁定,就像这样:
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));
}
}
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));
}
}