当前位置: 首页 > 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/14568148/

相关文章:

  • 网站建设在哪里接单wordpress更新 无法创建目录
  • 小学学校网站模板网站域名根目录在哪里
  • dedecms 做门户网站西安做seo的公司
  • 建设网站需要什么要求江苏定制网站建设费用
  • 怎样加入网站跨国网站怎么做
  • 济南网站制作公司哪家好网站域名注册流程
  • 非盈利性备案网站 淘宝客网站视频模板网
  • da面板做两个网站简约网站内容布局
  • 厦门模板建站系统网站开发项目挣钱吗
  • 怎么制作网站源码苏州网页设计方法
  • 做图去哪个网站找素材专门做鞋子的网站有哪些
  • 酒仙网网站推广方式类似设计师联盟的网站
  • 网站建设什么原因最主要制作个人网站实例
  • 优秀网站建设评选活动海报汕头seo优化项目
  • 网站开发 需求调研做网站ui
  • 福田做网站哪家专业怎么建立一个网站让外国人浏览
  • 做网站哪个软件好wordpress分类目录前缀
  • 中文网站站内优化怎么做网站建设收费分几次
  • 网站seo优化书籍怎样制作网页链接教程
  • 做网站 淘宝烟台网站建设seo
  • 广州商城建站系统外贸网站建设网站
  • 怎样建立一个企业网站天津手网站开发
  • 网站建设的技术亮点酒店协会网站集静态模板
  • 网站seo优化加推广支付宝小程序代理
  • 邮件模板网站怎么做好手机网站开发
  • 网页制作与网站设计思路wordpress电影站
  • 做网站教程流程网络营销策略4p4c
  • 微信官方网站首页域名服务费多少钱一年
  • 手机微网站第二年续费吗哪个平台推广效果最好
  • 郑州做网站公做的最好的相亲网站有哪些