zoukankan      html  css  js  c++  java
  • boost 线程(转)boost库timed_wait、notify_one配合使用

    #include "stdafx.h"
    #include <boost/thread/thread.hpp>
    #include <boost/thread/mutex.hpp>
    #include <boost/thread/condition.hpp>
    #include <boost/date_time/posix_time/posix_time.hpp>
    #include <iostream>
    #include<windows.h>
    using namespace std;
    using namespace boost;
    boost::mutex io_mutex;
    boost::condition_variable_any test_condition;

    void test(int num)
    {
    for (;;)
    {
    boost::mutex::scoped_lock lock(io_mutex);
    cout << "等待者" << num << endl;
    //阻塞等待30s,阻塞时自动解锁
    if ( test_condition.timed_wait(lock, get_system_time() + posix_time::seconds(30) ) )
    {
    cout << num << "成功接收到通知" << endl;
    } else
    {
    cout << num << "没有等待到通知" << endl;
    }
    }
    }
    void notify()
    {
    while(1)
    {
    cout << "发送通知"<<endl;
    system("pause");
    //test_condition.notify_all();
    test_condition.notify_one();
    }
    }

    int main(int argc, char* argv[])
    {
    boost::thread thrd11(bind(test, 1));
    boost::thread thrd12(bind(test, 2));
    boost::thread thrd13(notify);
    thrd11.join();
    thrd12.join();
    thrd13.join();

    system("pause");
    return 0;
    }

    原文地址:https://blog.csdn.net/leeboy_wang/article/details/44172193/

    不为其他,只为快乐!
  • 相关阅读:
    event 事件 键盘控制div移动
    event 事件 div鼠标跟随
    获取坐标封装 getPos
    event 事件 clientX 和clientY 配合scrollTop使用, div跟着鼠标走
    event 事件 冒泡
    event 事件 坐标兼容
    event事件基础 document
    DOM 多字符搜索
    DOM search table 模糊搜索
    Reverse a sentence
  • 原文地址:https://www.cnblogs.com/1521299249study/p/14441384.html
Copyright © 2011-2022 走看看