当前位置: 首页 > news >正文

做网站最好的网络公司chrome谷歌浏览器

做网站最好的网络公司,chrome谷歌浏览器,国内最新新闻热点事件,佛山做网站公司有哪些场景 在开发多线程程序时&#xff0c;有时候需要启动一个线程来监听外部进程的执行情况&#xff0c;并且在指定时间如果还没运行结束就强制结束外部线程。那么C标准库有这种监听线程并能在超时时提示的方法吗&#xff1f; 说明 在C11的<condition_variable>里就可以用…

场景

  1. 在开发多线程程序时,有时候需要启动一个线程来监听外部进程的执行情况,并且在指定时间如果还没运行结束就强制结束外部线程。那么C++标准库有这种监听线程并能在超时时提示的方法吗?

说明

  1. C++11<condition_variable>里就可以用条件变量来等待信号通知, 并设置超时时间。 超时时间的含义是,wait_for在超时达到时会自动唤醒mutex并不断尝试获取锁,当锁被获取时,进入下一条代码。
template< class Lock, class Rep, class Period >std::cv_status wait_for( Lock& lock,const std::chrono::duration<Rep, Period>& rel_time );
mutex.lock();
cond.wait_for(mutex, std::chrono::seconds(10));
mutex.unlock();
  1. wait_for的重载函数还有一个返回值是bool的函数对象。这个函数对象是为了避免虚假的唤醒,比如被错误的notify_one唤醒时,需要判断是否返回true, 如果返回true, 那么结束等待,否则继续等到到超时。 这个带Predicate参数的实现等同于
    wait_until(lock, std::chrono::steady_clock::now() + rel_time, std::move(pred));.[1]
template< class Lock, class Rep, class Period, class Predicate >bool wait_for( Lock& lock, const std::chrono::duration<Rep, Period>& rel_time,Predicate pred );
  1. 以上的Predicate函数对象的作用等同于以下的实现:[2].
  • 进入等待前先判断Predicate是否为true, 如果为false,进度等待。
  • 如果等到超时,直接返回Predicate的值,即执行下一行代码。
  • 如果非超时的唤醒,那么返回第一步。
while (!pred())if (wait_until(lock, abs_time) == std::cv_status::timeout)return pred();
return true;.
  1. 注意,使用条件变量唤醒时,不需要加锁。cond1.notify_one(); [3]因为如果对通知操作进行加锁,那么通知发生时,等待线程唤醒后会尝试获取锁,但是获取不到会迅速阻塞,因为被通知的线程需要等待通知线程解锁。
The notifying thread does not need to hold the lock on the same mutex as the one held by the waiting thread(s); 
in fact doing so is a pessimization, since the notified thread would immediately block again, 
waiting for the notifying thread to release the lock. 
However, some implementations (in particular many implementations of pthreads) recognize this situation and 
avoid this "hurry up and 
wait" scenario by transferring the waiting thread from the condition variable's queue directly to the queue of the mutex within the notify call, without waking it up. 

例子

// test-wait-for.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <condition_variable>
#include <thread>
#include <mutex>
#include <chrono>using namespace std;void work_1(condition_variable_any& cond,mutex& mutex)
{mutex.lock();cond.wait_for(mutex, std::chrono::seconds(10));mutex.unlock();
}void TestWaitFor_1()
{condition_variable_any cond1;mutex mutex1;auto beg =  std::chrono::system_clock::now();thread t1(work_1,std::ref(cond1),std::ref(mutex1));std::this_thread::sleep_for(std::chrono::seconds(3));//cond1.notify_one();t1.join();std::chrono::duration<double> seconds = std::chrono::system_clock::now() - beg;printf("TestWaitFor_2 elapsed_seconds: %fs\n", seconds);
}void work_2(condition_variable_any& cond,mutex& mutex,bool& bFinish)
{mutex.lock();cond.wait_for(mutex, std::chrono::seconds(10), [&bFinish]{return bFinish; });mutex.unlock();
}void TestWaitFor_2(bool bValue)
{condition_variable_any cond1;mutex mutex1;bool bFinish = false;auto beg =  std::chrono::system_clock::now();thread t1(work_2,std::ref(cond1),std::ref(mutex1),std::ref(bFinish));std::this_thread::sleep_for(std::chrono::seconds(3));// 用来处理虚假的唤醒,即如果没有到timeout就收到唤醒且bFinish还为false的话,wait会继续。bFinish = bValue; // 想让notify_one的唤醒生效,必须bFinish = true;cond1.notify_one();t1.join();std::chrono::duration<double> seconds = std::chrono::system_clock::now() - beg;printf("TestWaitFor_2 elapsed_seconds: %fs\n", seconds);
}int main()
{std::cout << "Hello World!\n";std::cout << "\n==== TestWaitFor_1 ====" << endl;TestWaitFor_1();std::cout << "\n=== TestWaitFor_2 ==== bFinish = false" << endl;TestWaitFor_2(false);std::cout << "\n=== TestWaitFor_2 ==== bFinish = true" << endl;TestWaitFor_2(true);
}

输出

Hello World!==== TestWaitFor_1 ====
TestWaitFor_2 elapsed_seconds: 10.012182s=== TestWaitFor_2 ==== bFinish = false
TestWaitFor_2 elapsed_seconds: 10.008821s=== TestWaitFor_2 ==== bFinish = true
TestWaitFor_2 elapsed_seconds: 3.030650s

参考

  1. wait_for

  2. wait_util

  3. notify_one

  4. pthread_cond_signal

http://www.mmbaike.com/news/23274.html

相关文章:

  • iis做网站百度网址大全在哪里找
  • 哪些网站是用响应式布局做的网络公关公司
  • 网站建设欧美网络营销方案总结
  • 百度云做网站空间发帖推广哪个平台好
  • 根据一个网站仿做新网站是什么网站网络营销推广要求
  • 国外网站怎么上优化落实新十条措施
  • 如何做网站预览2022拉新推广赚钱的app
  • 注册一个做网站的公司好福建seo优化
  • seo做的比较好的网站百度爱采购客服电话
  • 如何判断网站有cdn加速百度资源分享网
  • 鹤壁做网站的网络公司app推广怎么做
  • 青岛网站建设哪家权威厦门seo关键词优化代运营
  • 查商标邯郸网站seo
  • 网站开发前期调研seo算法培训
  • 武汉做网站互云网站制作免费
  • 成都有哪些做网站开发的大公司西地那非片的功能主治和副作用
  • 兰州市一地发布提醒网站怎么优化推荐
  • 如何做双版网站北京谷歌seo公司
  • 网站建设发布平台2022年可以打开的网址
  • 网站备案完成后接下来怎么做网址域名注册
  • 网站后台程序推广网站的方法
  • 做网站建设工资高吗理发美发培训学校
  • 做同城服务网站比较成功的网站加快百度收录的方法
  • 宿迁做网站百度网盘下载安装
  • 做 淘宝客最大的网站是叫什么bing搜索引擎国内版
  • 网站的建设费用分为上海app定制开发公司
  • 专业做模具钢的网站黄页网站推广app咋做广告
  • 陕西省住房和城乡建设厅执业资格注册中心网站网站秒收录工具
  • 阿里云上做网站友情贴吧
  • 做淘宝还是京东还是做网站网站搜索排名