#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;
}
我实现了多阅读器/单写入器示例。
问题是,一旦写入器拥有互斥锁或读取器拥有互斥锁,则不会将其权限转移到其相反的线程(readers->writer/writer->readers(
因此,程序的输出可以是两个之一。
当作家拿到锁时
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
更可取。
问题是你有紧密的循环,一旦另一个人抓住了互斥锁,读取器或写入器都无法轻易克服。看看你的循环要点:
- 锁定互斥锁
- 睡
- 释放互斥锁
- 转到步骤 1
而步骤 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));
}
}