[英]jthread vs std::async - what is the use-case for jthread over std::async
I have just stumbled accross std::jthread: https://en.cppreference.com/w/cpp/thread/jthread我刚刚偶然发现了 std::jthread: https://en.cppreference.com/w/cpp/thread/jthread
Which seems to be solving the same problem as std::async/future has already solved (although you may need to force the behaviour of async std::async(std::launch::async, ...
to run immediately.这似乎解决了与 std::async/future 已经解决的相同问题(尽管您可能需要强制 async
std::async(std::launch::async, ...
的行为立即运行。
So my question is what is the point of using one over the other?所以我的问题是使用一个而不是另一个有什么意义? - is there some difference?
- 有什么不同吗? is jthread a wrapper of async/future?
jthread 是异步/未来的包装器吗?
Abstractly, they are very similar.抽象地说,它们非常相似。 Assume that one uses
std::launch::async
and one does not call std::jthread::detach
.假设一个使用
std::launch::async
而一个不调用std::jthread::detach
。 They both accept synchronous functions and run them concurrently with the current thread of execution.它们都接受同步函数并与当前执行线程同时运行它们。 They both provide a way to wait on the completion:
std::future::get
or std::jthread::join
.它们都提供了一种等待完成的方法:
std::future::get
或std::jthread::join
。 They both implicitly and automatically perform the wait on destruction.它们都隐式地自动执行销毁等待。
The differences are more about the particular details and guarantees.差异更多地在于特定的细节和保证。
It should be noted that std::jthread
was added as a fix for std::thread
.应该注意的是,添加了
std::jthread
作为对std::thread
的修复。 std::thread
was added in C++11 alongside std::async
, and the same question is valid for these. std::thread
与std::async
一起添加到 C++11 中,同样的问题对这些有效。 So, std::async
had not "already" solved the problem, they were released together, as far as is relevant for this question.因此,
std::async
还没有“已经”解决了这个问题,它们是一起发布的,就与这个问题相关而言。 The problem with std::thread
was that it required the user to always join
or detach
the thread, detach
was always wrong, and if the user forgot to join
, the program would terminate. std::thread
的问题在于它要求用户始终join
或detach
线程, detach
总是错误的,如果用户忘记join
,程序将终止。
There is a meaningful difference in how values are returned.返回值的方式存在显着差异。
std::async
must implicitly allocate a shared location for the return value. std::async
必须为返回值隐式分配一个共享位置。 That isn't always necessary: sometimes you don't need a value, or sometimes you know that the asynchronous operation will complete before the current function returns (so, a captured stack variable suffices).这并不总是必要的:有时您不需要值,或者有时您知道异步操作将在当前 function 返回之前完成(因此,捕获的堆栈变量就足够了)。
There is a meaningful difference between std::future
s returned from std::async
and those constructed some other way.从
std::async
返回的std::future
与以其他方式构造的那些之间存在有意义的区别。 The ones returned from std::async
block on destruction, which is not the typical behavior.销毁时从
std::async
块返回的那些,这不是典型的行为。 This complicates both the implementation and the mental model of std::future
.这使
std::future
的实现和心理 model 变得复杂。
There is a meaningful difference in how exceptions are handled.处理异常的方式有很大的不同。 Exceptions are propagated back with
std::async
.异常通过
std::async
传播回来。
Therefore, the comments are spot on.因此,评论是当场的。
std::async
can easily be implemented with std::thread
or std::jthread
so it is at a higher level. std::async
可以很容易地用std::thread
或std::jthread
实现,因此它处于更高的级别。 But conversely, std::async
makes implicit choices which are not always optimal.但相反,
std::async
会做出并不总是最优的隐式选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.