boost::this_thread::sleep_for()会死锁

(金庆的专栏)

发现睡眠1ms很容易死锁。
boost::this_thread::sleep_for(boost::chrono::milliseconds(1)).

Boost 1.54.0

以下代码很可能重现死锁:

#include "stdafx.h"

#include <iostream>
#include <boost/thread.hpp>

using namespace std;

void worker()
{
    for (int i = 0; i < 10000; i++)
        boost::this_thread::sleep_for(boost::chrono::milliseconds(1));
}

int _tmain(int argc, _TCHAR* argv[])
{
    boost::thread_group tg;
    for (int i = 0; i < 30; i++)
        tg.create_thread(worker);

    tg.join_all();
    cout << "All done!" << endl;
    return 0;
}

附加到死锁进程调试时,调用栈如下:
boost::this_thread::interruptible_wait() Line 547
boost::detail::basic_cv_list_entry::wait() Line 94
boost::detail::basic_condition_variable::do_wait<>() Line 228
boost::condition_variable::wait_until<>() Line 371
boost::this_thread::sleep_until<>() Line32
boost::this_thread::sleep_for<>() Line72
worker()
boost::detail::thread_data<>()

程序在停在thread.cpp:547:
    unsigned long const notified_index=detail::win32::WaitForMultipleObjects(handle_count, handles, false, using_timer?INFINITE:time_left.milliseconds);
其中    
handle_count = 2
using_timer = false
time_left.milliseconds = 0xfffffffe

发现 basic_cv_list_entry::wait(timeout abs_time) 当 abs_time.milliseconds = uint64(-1) 时就会死锁.
所在可以在此断点,将 abs_time.milliseconds 改为 uint64(-1),就可以重现死锁了.

实际上该参数很可能输入为-1:
        wait_until(...)
        {
          do_wait(lock, ceil<milliseconds>(t-Clock::now()).count());
          ...
        }

这几天改服务器代码,添加了许多 sleep_for(), 结果就经常发现服务器停止响应了。
幸好该错误仅在Windows上存在。错误位于 boost/thread/win32/condition_variable.hpp.

正准备给Boost添加条Bug记录,发现该错误已在3周前报告,10天前已修正,将在1.55.0版本中发布。
见:
Condition variable will wait forever for some timepoint values (MSVC)    
https://svn.boost.org/trac/boost/ticket/9079

修正代码如下:

    chrono::time_point<Clock, Duration> now = Clock::now();
    if (t<=now) {
      return cv_status::timeout;
    }
    do_wait(lock, ceil<milliseconds>(t-now).count());

boost::this_thread::sleep_for()死锁的更多相关文章

  1. 错误 4 error C2039: “Sleep”: 不是“boost::this_thread”的成员

    检查是否是在x64下运行的. #include <pcl/io/openni2_grabber.h> #include <pcl/visualization/cloud_viewer ...

  2. boost和C++11中的sleep

    boost boost线程中表示睡眠的函数有sleep和sleep_for sleep 例如: boost::this_thread::sleep(boost::posix_time::seconds ...

  3. (十)boost库之多线程

    (十)boost库之多线程 1.创建线程 使用boost库可以方便的创建一个线程,并提供最多支持9个参数的线程函数,相对于void*来说,方便了很多,创建线程主要提供了一下3种方式: 线程库头文件:# ...

  4. http客户端-基于boost开发

    http客户端-基于boost开发 基于BOOST编写的http客户端,作为BOOST开发学习之用.目前支持功能: http协议,单向链接返回http response code 200 可conte ...

  5. BOOST学习笔记

    BOOST学习笔记 1 tool #pragma once #include <vector> #include "boost/noncopyable.hpp" #in ...

  6. Debug EOS:nodeos + mongo_db_plugin

    上文书说到区块链的存储方式,并结合了EOSIO进行分析,其中也提到了使用CLion调试EOS的方法.本文将继续深入细致地展开对加载了mongo_db_plugin的nodeos的调试过程以及心得. 关 ...

  7. c++并发编程之条件变量(Condition Variable)

    条件变量(Condition Variable)的一般用法是:线程 A 等待某个条件并挂起,直到线程 B 设置了这个条件,并通知条件变量,然后线程 A 被唤醒.经典的「生产者-消费者」问题就可以用条件 ...

  8. 搞定thrift双向消息

    thrift作为脱胎于facebook的rpc框架,各方面都非常优秀.清晰的分层设计,多语言的支持,以及不输protocolbuffer的效率(compact下优于protocolbuffer),都让 ...

  9. std::this_thread::yield/sleep_for

    std::this_thread::yield(): 当前线程放弃执行,操作系统调度另一线程继续执行.. std::this_thread::sleep_for(): 表示当前线程休眠一段时间,休眠期 ...

随机推荐

  1. Storm 0.9安装指南

    Storm 0.9.2安装指南 0 Storm0.9的亮点 引用网上的描述: "Storm 0.9.0.1版本的第一亮点是引入了netty transport.Storm网络传输机制实现可插 ...

  2. 【翻译】Ext JS 6.2 早期访问版本发布

    原文:Announcing Ext JS 6.2 Early Access 非常开心,Sencha Ext JS 6.2早期访问版本今天发布了.早期访问版本的主要目的是为了让大家进行测试并评估Ext ...

  3. 转换xml格式的短信记录

    <?xml version="1.0" encoding="UTF-8"?> <SMSRecord>     <SMS>   ...

  4. Bootstrap模板代码+页面自适应页面的案例代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  5. 【一天一道LeetCode】#94. Binary Tree Inorder Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. 【FPGA学习】Verilog之加法器

    在fpga工程应用设计中,随处可见加法器,乘法器等等.现在将一些常用模块和心得体会先记录下来,以便日后使用. 一位半加器: module halfadder(cout,sum,a,b); output ...

  7. 2015&nbsp;Objective-C&nbsp;三大新特性

    Overview 自 WWDC 2015 推出和开源 Swift 2.0 后,大家对 Swift 的热情又一次高涨起来,在羡慕创业公司的朋友们大谈 Swift 新特性的同时,也有很多像我一样工作上依然 ...

  8. Android 官方命令深入分析之Android Debug Bridge(adb)

    作者:宋志辉 Android Debug Brideg(adb)是一个多用途的命令行工具.可以与Android虚拟机进行通信或连接真机.它同样提供了访问设备shell的高级命令行操作的权限.它是一个包 ...

  9. chrome "Provisional headers are shown"

    我的问题的,每次打开浏览器,点开有视频的页面(云平台的存储),然后当视频还没有加载完,就马上关闭,连续操作很多次(测试的暴力测试把),F12查看就有很多很多在加载中的连接,因为连续点击太多次了,第一个 ...

  10. memcached /usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory

    启动memcached的时候发现找不到libevent的库,这是memcache的默认查找路径不包含libevent的安装路径,所以要告诉memcached去哪里查找libevent. 操作命令如下: ...