[英]Ownership of mutex is not transferred to another thread
#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;
}
我实现了多读者/单作家示例。
问题是一旦作家拥有互斥锁或读者拥有互斥锁,所有权就不会转移到其相反的线程(读者->作家/作家->读者)
因此,程序的输出可以是两个之一。
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
更可取。
问题是您的循环很紧,一旦其他人抓住了互斥量,读者或作家就无法轻易克服。 看看您的循环要点:
第三步之后的窗口是读者或作家抓住互斥锁的唯一机会。 该窗口很短,因此实际抓住它的机会很小。 这就是为什么您只能在控制台上看到作家或读者的原因。 实际上,如果您永远等待,您很可能会看到不同的实体将有机会工作。
那么如何解决呢? 这很简单:只需将睡眠从锁中移出,就像这样:
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));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.