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

建设银行网站优点个人app

建设银行网站优点,个人app,网页设计模板html代码个人介绍,淘宝权重查询为什么写这个 C17异步任务需要future和promise配合使用#xff0c;不是很喜欢那种语法。实现一个操作简洁的异步任务。 满足功能 异步任务超时控制get接口同步任务计时lambda回调任务重启 使用 #include async_callback.h #include unistd.h #includ…为什么写这个 C17异步任务需要future和promise配合使用不是很喜欢那种语法。实现一个操作简洁的异步任务。 满足功能 异步任务超时控制get接口同步任务计时lambda回调任务重启 使用 #include async_callback.h #include unistd.h #include iostream using namespace std;int main() {int a 0, b 0, c 0;AsyncTask* task1 new AsyncTask([a](){sleep(2);for (int i 0; i 10000; i) {a;}});AsyncTask* task2 new AsyncTask([b](){sleep(2);for (int i 0; i 10000; i) {b;}});if (!task1-get(1)) {cout task1超时 endl;}if (task2-get()) {cout task2没超时 endl;}task1-restart();task2-restart();task1-get();task2-get();cout a endl;cout b endl;cout task1执行时间: task1-executionTime() endl;cout task2执行时间: task2-executionTime() endl;return 0; }打印结果 task1超时 task2没超时 20000 20000 task1执行时间:2.00009 task2执行时间:2.00009代码 // Simple asynchronous tasks with timeout // Author: Y. F. Zhang // Date: 2023-09-21#ifndef ASYNC_CALLBACK_H #define ASYNC_CALLBACK_H#include mutex #include condition_variable #include thread #include atomic #include functional #include exception #include iostream #include sys/time.h #include time.h class Timer { public:Timer() {offsetTime_ 0;timerState_ TIMERSTOP;}void timerStart() {timerState_ TIMERRUN;timeStamp_ getWallTime();}double elapsedTime() {if (timerState_ TIMERPAUSE) {return offsetTime_;}curTime_ getWallTime() - timeStamp_ offsetTime_;if (timerState_ TIMERSTOP) return 0;return curTime_;}void pauseTimer() {offsetTime_ getWallTime() - timeStamp_; timerState_ TIMERPAUSE;}void stopTimer() {offsetTime_ 0;curTime_ 0;timerState_ TIMERSTOP;}private:double getWallTime() {struct timeval time ;if (gettimeofday(time,NULL)){return 0;}return (double)time.tv_sec (double)time.tv_usec * .000001;}double timeStamp_;double curTime_;double offsetTime_;enum{TIMERSTOP,TIMERRUN,TIMERPAUSE} timerState_; };namespace AsyncTaskException {class GetButTaskStopErr: public std::exception {const char* what() const throw () {return invoke get method but task is stop!;} };class GetExecutionTimeButTaskStopErr: public std::exception {const char* what() const throw () {return invoke executionTime method but task is stop!;} };}using func std::functionvoid(); class AsyncTask { private:enum {TASKRUNNING,TASKSTOP,TASKFINISHED} taskState_;func callback_;std::mutex mtx_;std::condition_variable cond_;std::atomic_bool completedFlag_;std::thread* taskThread_;Timer timer_;void initTask() {timer_.stopTimer();taskState_ TASKSTOP;completedFlag_.store(false);if (taskThread_ ! nullptr) {delete taskThread_;taskThread_ nullptr;}}void runTask() {auto wrapperCallback [this]() {try {timer_.timerStart();callback_();timer_.pauseTimer();completedFlag_.store(true);} catch (std::exception e) {fprintf(stderr, %s, e.what());}taskState_ TASKFINISHED;cond_.notify_one();};taskState_ TASKRUNNING;taskThread_ new std::thread(wrapperCallback);taskThread_-detach();} public:AsyncTask(func callback) {this-callback_ callback;restart();}void restart() {initTask();runTask();}double executionTime() {if (taskState_ TASKSTOP) {throw AsyncTaskException::GetExecutionTimeButTaskStopErr();}return timer_.elapsedTime();}~AsyncTask() {if (taskThread_ ! nullptr) {delete taskThread_;}}bool get(size_t timeoutSec 0) {if (taskState_ TASKSTOP) {throw AsyncTaskException::GetButTaskStopErr();}std::unique_lockstd::mutex lk(mtx_);if (timeoutSec 0) {cond_.wait(lk, [this](){return completedFlag_.load();});} else {return cond_.wait_for(lk, std::chrono::seconds(timeoutSec), [this](){return completedFlag_.load();});}return true;}};#endif
http://www.w-s-a.com/news/539515/

相关文章:

  • 长春网站建设公司怎么样电商网站建设与开发期末考试
  • 品牌网站建设搭建国内外网站建设
  • 辽宁人社app一直更新整站seo定制
  • 兰州网站建设论坛装修品牌
  • 云南省城乡住房与建设厅网站用什么网站可以做电子书
  • 自己电脑怎么做网站服务器吗0基础如何做网站
  • 做网站的股哥网络整合营销方案策划
  • 网站你懂我意思正能量晚上唯品会网站开发费用
  • 网站认证金额怎么做分录网页无法访问是怎么回事
  • 樟木头建网站的wordpress自适应吸附菜单
  • 番禺网站设计威海微网站建设
  • 新乡网站建设服务网站建设的点子
  • 赛罕区城乡建设局网站什么是新媒体运营
  • 松原企业网站建设设计素材网排名
  • 网站建设是那个行业广东公司排名
  • 制作网站要多少钱seo是如何优化
  • 求个网站2020急急急做金融网站拘留多久
  • 网站后台管理系统怎么进seo网络推广外包公司
  • 中山市 做网站网站建设如何上传文件
  • 网站呢建设公众号制作要求
  • 网站备案证明在自己电脑上做网站
  • 沈阳旅游团购网站建设怎么制作网站搜索窗口
  • 做化学合成的网站有哪些枣庄住房和城乡建设局网站
  • 天猫优惠券网站怎么做的网络连接
  • 保定网站建设多少钱公司网页网站建设+ppt模板下载
  • 用户上传商品网站用什么做建设跳转公积金网站
  • 买程序的网站上海市网站建设公司
  • 南通网站建设排名公司哪家好wordpress网站图片迁移
  • 河南省汝州文明建设门户网站博客网站建设源码
  • 单位建设网站的请示手机移动端网站案例