简体   繁体   English

我可以使用 std::stop_source 从工作线程中延迟取消所有工作线程吗?

[英]Can I use std::stop_source to deferred cancel all worker threads from a worker thread?

I have two threads running in parallell started from the main thread.我有两个从主线程开始并行运行的线程。 Either thread may fail.任何一个线程都可能失败。 If it do so I want to ask the other thread to cancel as well.如果这样做,我也想要求另一个线程取消。 In C++ 20 there is std::stop_token and std::stop_source .在 C++ 20 中有std::stop_tokenstd::stop_source I have found several examples where the main thread ask all threads to cancel (deferred cancelation).我发现了几个例子,其中主线程要求所有线程取消(延迟取消)。 However I have not found any examples where either worker thread may deferred cancel all other worker threads.但是,我还没有找到任何一个工作线程可以推迟取消所有其他工作线程的例子。 How is this done?这是怎么做到的?

In order for a location of code to be able to request a stop, that location must have a std::stop_source .为了使代码位置能够请求停止,该位置必须具有std::stop_source In order for a location of code to be able to respond to the stop, it must have a std::stop_token taken from a particular stop_source .为了使代码位置能够响应停止,它必须具有从特定stop_source获取的std::stop_token stop_token 。

Therefore, in order for a location of code to be able to both request and respond to a stop, it must have both the stop_source and the stop_token .因此,为了使代码位置能够请求和响应停止,它必须同时具有stop_sourcestop_token

Now, since stop_source s are able to generate stop_token s, there's no reason why you can't just give the thread function a stop_source and have it extract a token from it.现在,由于stop_source能够生成stop_token ,所以没有理由不能只给线程 function 一个stop_source并让它从中提取令牌。 Of course, this won't be able to use std::jthread s convenience ability to detect that a thread function whose first argument is a stop_token and generate a stop_source , but the nature of your stopping needs wouldn't make that viable anyway.当然,这将无法使用std::jthread的便捷功能来检测第一个参数是stop_token并生成 stop_source 的线程 function 并生成stop_source ,但是您的停止需求的性质无论如何都不会使其可行。

Also, remember that stop_source is copyable ;另外,请记住stop_source是可复制的; the copies all share the same internal stop-state (along with any stop_token s created from them).这些副本都共享相同的内部停止状态(以及从它们创建的任何stop_token )。 So there's no reason to pass them by reference.所以没有理由通过引用传递它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 std::stop_source 和 std::stop_token 代替 std::atomic 的好处<bool>延期取消?</bool> - Benefits of using std::stop_source and std::stop_token instead of std::atomic<bool> for deferred cancellation? C ++,pthreads:如何从多个线程停止工作线程 - C++, pthreads: how to stop a worker thread from multiple threads 将工作线程与主线程同步 - Synchronize worker threads with a main thread MFC:从工作线程向主线程发送消息,以停止工作线程 - MFC: sending a msg from worker thread to the Main thread to stop the worker thread 工作线程如何与主UI线程通信? - How can worker threads communicate with main UI thread? Qt从GUI线程停止工作线程循环 - Qt Stop worker thread loop from GUI Thread 从线程池工作线程使用GetQueuedCompletionStatus的奇怪行为 - Strange behaviour of GetQueuedCompletionStatus when used from thread pool worker threads 如何在主线程的 WaitForMultipleObjects 调用中间安全地终止工作线程? - How can I safely terminate a worker thread in the middle of a WaitForMultipleObjects call from the main thread? C ++:如何在UI线程和工作程序std :: thread之间使用std :: condition_variable - C++ : How to use an std::condition_variable between UI thread & worker std::thread 无法将接口从主线程编组到工作线程 - Can not marshal interface from main thread into worker thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM