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

域名 和网站有什么区别天马网络网站

域名 和网站有什么区别,天马网络网站,企业网页设计,详细描述建设网站场景 在开发多线程程序时#xff0c;有时候需要启动一个线程来监听外部进程的执行情况#xff0c;并且在指定时间如果还没运行结束就强制结束外部线程。那么C标准库有这种监听线程并能在超时时提示的方法吗#xff1f; 说明 在C11的condition_variable里就可以用…场景 在开发多线程程序时有时候需要启动一个线程来监听外部进程的执行情况并且在指定时间如果还没运行结束就强制结束外部线程。那么C标准库有这种监听线程并能在超时时提示的方法吗 说明 在C11的condition_variable里就可以用条件变量来等待信号通知, 并设置超时时间。 超时时间的含义是wait_for在超时达到时会自动唤醒mutex并不断尝试获取锁当锁被获取时进入下一条代码。 template class Lock, class Rep, class Period std::cv_status wait_for( Lock lock,const std::chrono::durationRep, Period rel_time );mutex.lock(); cond.wait_for(mutex, std::chrono::seconds(10)); mutex.unlock();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::durationRep, Period rel_time,Predicate pred );以上的Predicate函数对象的作用等同于以下的实现:[2]. 进入等待前先判断Predicate是否为true, 如果为false,进度等待。如果等到超时直接返回Predicate的值即执行下一行代码。如果非超时的唤醒那么返回第一步。 while (!pred())if (wait_until(lock, abs_time) std::cv_status::timeout)return pred(); return true;.注意使用条件变量唤醒时不需要加锁。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 variables 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 chronousing 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::durationdouble 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::durationdouble 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参考 wait_for wait_util notify_one pthread_cond_signal
http://www.hkea.cn/news/14490775/

相关文章:

  • 哈尔滨网站建设 seo广东个人备案网站内容
  • 班级网站策划书天津技术网站建设
  • 建设工程消防信息网站旅游网页模板免费下载
  • 做暖暖视频免费观看免费网站wordpress 伪静态 后台
  • 阿里云虚拟主机做企业网站深圳网站备案拍照点
  • 分析企业网站建设流程平面设计必备软件
  • 网站建设怎样推广做网站跟app
  • 社区网站推广方案培训机构参与课后服务
  • 网站规划设计是什么样的排名优化系统
  • 做淘宝客网站需要多大空间什么网站可以做特价活动
  • 浙江建设网站公司外贸企业网站推广方案
  • 淘宝网站建设不允许西安企业网站开发哪家好
  • 南宁旅游网站建设wordpress远程附件
  • 网站管理的内容包括怎样才能建立网站
  • 做网站对服务器什么要求高asp企业网站源码下载
  • html5网站代理建网站的服务器
  • 宁海县城镇建设局网站wordpress 简洁文章主题
  • 网站运营推广难做吗网络推广软件哪个好
  • 在哪个网站做流动补胎的广告好建立企业网站方案
  • 古典风格中药医药企业网站模板源码flash个人网站动画
  • 广州购物网站开发php网站开发技巧
  • 电子商务网站建设过程报告导购网站如何做免费推广
  • 给个网站可以在线wordpress导航主题模板
  • win10 做网站服务器潜江资讯网官网
  • 网站建设 计入哪个科目wordpress开头
  • 蚌埠 网站制作济南建设档案大厦
  • 东莞网站建设曼哈顿信科wordpress页面文本编辑评论
  • 四川seo整站优化费用分类信息网
  • 小型企业类网站开发公司建设网站多少钱 2017
  • 手机网站 制作深圳 购物商城网站建设