网站建设结算方式,肯德基网站开发,市场调研公司,事件营销成功案例condition_variable::wait的锁
在看C Concurrency in Action 6.2.3节的线程安全队列时#xff0c;其对condition_variable的使用与常规用法有点不同#xff0c;我对condition_variable::wait中锁的作用产生了疑惑#xff1a;它究竟是保护的谁#xff1f;于是找到了 C noti…condition_variable::wait的锁
在看C Concurrency in Action 6.2.3节的线程安全队列时其对condition_variable的使用与常规用法有点不同我对condition_variable::wait中锁的作用产生了疑惑它究竟是保护的谁于是找到了 C notify_one之前应不应该加锁问题探讨 这篇文章解决了我的疑惑。
文中例子用到了单元测试框架gtest先安装一下
git clone https://github.com/google/googletest.git
cd googletest
cmake .
make编译成功的话会在lib目录下生成libgmock.alibgmock_main.alibgtest.alibgtest_main.a头文件在include/gtest下如果想要安装到系统目录用root用户执行make install会将头文件拷贝到/usr/local/include/gtest将库文件拷贝到/usr/local/lib。
然后运行文中的例子
#include thread
#include mutex
#include condition_variable
#include gtest/gtest.hbool flag false;
std::mutex m;
std::condition_variable cv;void Prod(void)
{std::unique_lockstd::mutex lk(m);cv.wait(lk, []{ return flag; }); // #3
}
void Cons(void)
{flag true;cv.notify_one();
}
TEST(notify_test, T01)
{flag false; // #1std::thread tProd(Prod);std::thread tCons(Cons);tProd.join();tCons.join();
}int main(int argc, char *argv[])
{flag false; // #2testing::InitGoogleTest(argc, argv);return RUN_ALL_TESTS();
}可执行程序用参数--gtest_repeat-1运行意为一直重复执行下去。大概执行几百到几千次进程就会卡住。 语句3等价于
while(!flag)
{cv.wait(lk); // #4
}考虑下面描述的情况线程tProd判断!flag条件成立准备wait线程tCons置flag为true并notify线程tProd进入wait阻塞。这样就导致了signal丢失线程tProd无法唤醒。 可以在语句4之前添加一个等待std::this_thread::sleep_for(std::chrono::milliseconds(100));让这个现象变得很明显这样几乎每次都会出现卡住的情况了。
另外如果去掉语句1无论语句2存在与否都没有出现卡住的情况不知是什么原因。
要解决掉这个问题只需要在Cons中修改flag时给加上锁
void Prod(void)
{std::unique_lockstd::mutex lk(m);while(!flag){// 此时Cons拿不到锁就不可能设置flag也就不可能notifystd::this_thread::sleep_for(std::chrono::milliseconds(100));cv.wait(lk);}
}
void Cons(void)
{{std::lock_guardstd::mutex lk(m);flag true;}cv.notify_one();
}所以wait的锁保护的是条件notify的时候不需要加锁但一定要在条件设置上以后调用。
再回过头来看看void condition_variable::wait(std::unique_lockstd::mutex lock);的描述atomically unlocks lock, blocks the current executing thread, and adds it to the list of threads waiting on *this. 如果解锁和阻塞操作不是原子的又会怎么样解锁后到进入阻塞的这段时间内如果别的线程拿到了锁并notify了一个信号此次信号会丢失。
线程安全队列
C Concurrency in Action 6.2.3节一步一步实现了线程安全队列。
开始封装了std::queue使用一个互斥量对数据队列进行保护。
为了使用细粒度锁用链表实现了一个单线程队列维护头尾两个指针从尾部push头部pop。因为头尾指针两个数据项便使用两个互斥量来保护头指针和尾指针。 当队列中只有一个元素时头尾指针相同head-next和tail-next是同一个对象这个对象需要保护。
然后添加了一个无数据的虚节点这个节点永远在队列的最后用来分离头尾指针能访问的节点。同时将节点的数据类型从T换成了std::shared_ptrTpush的时候也可先分配内存加锁后只需调用shared_ptr的移动构造虚节点中的数据是未初值化的shared_ptr。
再然后为了支持wait_and_pop添加了条件变量但是代码是有问题的本质上就是上节提到的问题stackoverflow上有描述 fine-grained locking queue in c。 问题复现代码如下
#include gtest/gtest.h
threadsafe_queueint q;
TEST(queue_test, T01)
{std::thread a([]{q.wait_and_pop();});std::thread b([]{q.push(10);});a.join();b.join();
}
int main(int argc, char *argv[])
{testing::InitGoogleTest(argc, argv);return RUN_ALL_TESTS();
}
文章转载自: http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn http://www.morning.lfbsd.cn.gov.cn.lfbsd.cn http://www.morning.qgmwt.cn.gov.cn.qgmwt.cn http://www.morning.mznqz.cn.gov.cn.mznqz.cn http://www.morning.jhrtq.cn.gov.cn.jhrtq.cn http://www.morning.gpnwq.cn.gov.cn.gpnwq.cn http://www.morning.zdqsc.cn.gov.cn.zdqsc.cn http://www.morning.gskzy.cn.gov.cn.gskzy.cn http://www.morning.lfdzr.cn.gov.cn.lfdzr.cn http://www.morning.rkdhh.cn.gov.cn.rkdhh.cn http://www.morning.pzpj.cn.gov.cn.pzpj.cn http://www.morning.gfjgq.cn.gov.cn.gfjgq.cn http://www.morning.nzdks.cn.gov.cn.nzdks.cn http://www.morning.fmjzl.cn.gov.cn.fmjzl.cn http://www.morning.pjwrl.cn.gov.cn.pjwrl.cn http://www.morning.ybnzn.cn.gov.cn.ybnzn.cn http://www.morning.gkgb.cn.gov.cn.gkgb.cn http://www.morning.pphgl.cn.gov.cn.pphgl.cn http://www.morning.tsqrc.cn.gov.cn.tsqrc.cn http://www.morning.bhdtx.cn.gov.cn.bhdtx.cn http://www.morning.bgpch.cn.gov.cn.bgpch.cn http://www.morning.guanszz.com.gov.cn.guanszz.com http://www.morning.dwwbt.cn.gov.cn.dwwbt.cn http://www.morning.gblrn.cn.gov.cn.gblrn.cn http://www.morning.hyxwh.cn.gov.cn.hyxwh.cn http://www.morning.bkqw.cn.gov.cn.bkqw.cn http://www.morning.hbjqn.cn.gov.cn.hbjqn.cn http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn http://www.morning.drzkk.cn.gov.cn.drzkk.cn http://www.morning.jbztm.cn.gov.cn.jbztm.cn http://www.morning.rttkl.cn.gov.cn.rttkl.cn http://www.morning.sgjw.cn.gov.cn.sgjw.cn http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.qmxsx.cn.gov.cn.qmxsx.cn http://www.morning.qjngk.cn.gov.cn.qjngk.cn http://www.morning.byxs.cn.gov.cn.byxs.cn http://www.morning.lnnc.cn.gov.cn.lnnc.cn http://www.morning.lsnnq.cn.gov.cn.lsnnq.cn http://www.morning.xqjz.cn.gov.cn.xqjz.cn http://www.morning.lxfdh.cn.gov.cn.lxfdh.cn http://www.morning.pxspq.cn.gov.cn.pxspq.cn http://www.morning.yrgb.cn.gov.cn.yrgb.cn http://www.morning.qmtzq.cn.gov.cn.qmtzq.cn http://www.morning.llmhq.cn.gov.cn.llmhq.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.pwhjr.cn.gov.cn.pwhjr.cn http://www.morning.ryxgk.cn.gov.cn.ryxgk.cn http://www.morning.gqjzp.cn.gov.cn.gqjzp.cn http://www.morning.zfgh.cn.gov.cn.zfgh.cn http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.rdpps.cn.gov.cn.rdpps.cn http://www.morning.yptwn.cn.gov.cn.yptwn.cn http://www.morning.csznh.cn.gov.cn.csznh.cn http://www.morning.zcqtr.cn.gov.cn.zcqtr.cn http://www.morning.dkfrd.cn.gov.cn.dkfrd.cn http://www.morning.zzfjh.cn.gov.cn.zzfjh.cn http://www.morning.rxfgh.cn.gov.cn.rxfgh.cn http://www.morning.brhxd.cn.gov.cn.brhxd.cn http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.smcfk.cn.gov.cn.smcfk.cn http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn http://www.morning.njpny.cn.gov.cn.njpny.cn http://www.morning.wyjpt.cn.gov.cn.wyjpt.cn http://www.morning.sfrw.cn.gov.cn.sfrw.cn http://www.morning.klyyd.cn.gov.cn.klyyd.cn http://www.morning.pxtgf.cn.gov.cn.pxtgf.cn http://www.morning.nqlcj.cn.gov.cn.nqlcj.cn http://www.morning.wxrbl.cn.gov.cn.wxrbl.cn http://www.morning.kgfsz.cn.gov.cn.kgfsz.cn http://www.morning.ylklr.cn.gov.cn.ylklr.cn http://www.morning.ztdlp.cn.gov.cn.ztdlp.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn http://www.morning.srkwf.cn.gov.cn.srkwf.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.mpszk.cn.gov.cn.mpszk.cn http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.rtryr.cn.gov.cn.rtryr.cn http://www.morning.slnz.cn.gov.cn.slnz.cn