如何中断Asynch线程
How to interrupt in asynch thread?
我正在尝试为我的项目制作一些异步线程。
,我想知道可以使用某种方式(例如中断)完成"任务5"。
是否可以在已经运行的'call_from_async'?
中更改ITV_Duration#include <iostream>
#include <future>
#include <vector>
using namespace std;
enum class status{
STATUS_REVICE = 0,
STATUS_START = 1,
STATUS_COMPLETED = 2,
STATUS_CANCELED = 3
};
int called_from_async(std::string taskName, int creationTime, int itv_duration, int itv_start)
{
cout << "creationTime : " << creationTime << endl;
cout << "interval : " << itv_duration << endl;
cout << "startTime : " << itv_start << endl;
cout << "status : 1 Event Recevied" << endl;
bool bExit = false;
bool bStart = false;
while(!bExit)
{
if(creationTime > itv_start && !bStart)
{
bStart = true;
}
creationTime++;
if(bStart)
{
itv_duration--;
if(itv_duration < 0 )
{
bExit = true;
}
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
cout << "status : 3 Event Completed : " << taskName.c_str() << endl;
return 1;
}
class eventManager
{
public:
eventManager() {};
~eventManager() {};
std::future<int> addTask(std::string taskName, int creationTime, int itv_duration, int itv_start)
{
return std::async(std::launch::async, called_from_async, taskName, creationTime, itv_duration, itv_start);
}
private:
};
int main() {
std::vector<std::future<int>> futures; // place for asyns tasks
eventManager evtManager;
futures.push_back(std::move(evtManager.addTask("Task 1", 1234560, 20, 1234570))); // will be done as 4th
futures.push_back(std::move(evtManager.addTask("Task 2", 1234560, 15, 1234570))); // will be done as third
futures.push_back(std::move(evtManager.addTask("Task 3", 1234560, 10, 1234570))); // will be done as second
futures.push_back(std::move(evtManager.addTask("Task 4", 1234560, 5, 1234570))); // will be done as first
futures.push_back(std::move(evtManager.addTask("Task 5", 1234560, 360, 1234570))); // super long task, but will be done as zero because of cancel event.
return 0;
您可以通过引用而不是按值来传递持续时间(在执行过程中要更改)。
创建一个专用变量来处理取消也是一个好主意。我在下面的示例中使用了一个简单的bool
,但是您可能会考虑更喜欢的东西,例如std::condition_variable
。
int called_from_async(std::string taskName, int creationTime, int& itv_duration, int itv_start, bool& cancel)
{
bool bExit = false;
bool bStart = false;
while (!bExit && !cancel)
{
if (creationTime > itv_start && !bStart)
{
bStart = true;
}
creationTime++;
if (bStart)
{
if (--itv_duration < 0)
{
bExit = true;
}
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
std::cout << taskName << std::endl;
return 1;
}
class eventManager
{
public:
eventManager() { };
~eventManager() { };
std::future<int> addTask(std::string taskName, int creationTime, int& itv_duration, int itv_start, bool& cancel)
{
return std::async(std::launch::async, [=, &itv_duration, &cancel]() { return called_from_async(taskName, creationTime, itv_duration, itv_start, cancel); });
}
};
int main()
{
std::vector<std::future<int>> futures; // place for asyns tasks
eventManager evtManager;
bool cancel1 = false, cancel5 = false;
int shortDuration = 2, longDuration = 360;
futures.push_back(std::move(evtManager.addTask("Task 1", 1234560, shortDuration, 1234570, cancel1))); // will be done as 4th
//...
futures.push_back(std::move(evtManager.addTask("Task 5", 1234560, longDuration, 1234570, cancel5))); // super long task, but will be done as zero because of cancel event.
longDuration = 1;
//cancel5 = true;
//wait for your tasks to finish, so that references are valid
//or declare them in the higher scope
return 0;
}
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- c++中的线程池
- 线程之间的布尔停止信号
- 为什么std::async使用同一个线程运行函数
- 用于矢量处理的多个线程
- C++为线程工作动态地分割例程
- 如何中断Asynch线程