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

像优酷这样的网站需要怎么做搜外网友情链接

像优酷这样的网站需要怎么做,搜外网友情链接,wordpress换不了密码,wordpress 禁止 字体大小为什么写这个 C17异步任务需要future和promise配合使用&#xff0c;不是很喜欢那种语法。实现一个操作简洁的异步任务。 满足功能 异步任务超时控制get接口同步任务计时lambda回调任务重启 使用 #include "async_callback.h" #include <unistd.h> #includ…

为什么写这个

  1. C++17异步任务需要future和promise配合使用,不是很喜欢那种语法。
  2. 实现一个操作简洁的异步任务。

满足功能

  1. 异步任务
  2. 超时控制
  3. get接口同步
  4. 任务计时
  5. lambda回调
  6. 任务重启

使用

#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::function<void()>;
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_lock<std::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.jinmujx.cn/news/112159.html

相关文章:

  • 郑州淘宝网站推广 汉狮网络软文代发代理
  • 360算互联网大厂吗seo入门教程
  • 南昌网站外包手机seo快速排名
  • 网站建设与管理.pdf网站运营培训学校
  • 北京网站建设价格天专注网站建设服务机构
  • 购物商城外贸网站搜索引擎优化策略不包括
  • 企业网站页脚品牌策划运营公司
  • 网站建设目录结构设计长沙网站推广排名
  • 211工程建设网站百度模拟点击
  • 绍兴公司网站建设 中企动力绍兴百度人工智能开放平台
  • php动态网站开发视频免费推广引流平台
  • app定制软件seo网站关键字优化
  • 怎样做淘宝网站百度首页网站推广多少钱一年
  • 如何选择企业网站建设想要导航页面推广app
  • 个人网站做装修可以吗网络营销专业技能
  • 网络营销为导向的企业网站深圳推广系统
  • 有哪些学做衣服的网站网络营销和电子商务区别
  • 淘宝客推广网站模板seo是什么学校
  • 598网站建设掌门一对一辅导官网
  • 网站制作在线版上海做网站优化
  • 公司邮箱怎么注册北京seo排名外包
  • 廊坊app网站制作网络营销的核心
  • 龙华网站建设招聘总裁培训班
  • 企业站用wordpress做好吗如何进行线上推广
  • 织梦app网站模板谷歌广告投放
  • html5 电商网站布局google关键词排名查询
  • 嘉定网站建设网页制作seo原创工具
  • 数据库与动态网站开发实验报告竞价托管信息
  • 唐山哪家网站好产品软文范例
  • 山东省工程建设管理协会网站营销软文小短文