[英]thread cancellation points in c++
Is there a concept of thread cancellation points in c++ just like in POSIX. c++ 中是否有线程取消点的概念,就像在 POSIX 中一样。 I personally felt it's very handful feature especially when terminating threads that got blocked when a blocking function is called(eg. socket
recvfrom
function).我个人觉得这是一个非常小的功能,特别是在终止调用阻塞 function 时被阻塞的线程时(例如 socket
recvfrom
函数)。
No, there is no direct equivalent.不,没有直接的等价物。
The closest thing is std::jthread
's std::stop_token
since C++20 which is a voluntary mechanism with which you can define points in the thread function at which the thread can be requested to terminate.最接近的是
std::jthread
的std::stop_token
自 C++20 以来,它是一种自愿机制,您可以使用它在线程 function 中定义可以请求线程终止的点。 You can wrap standard library or platform-specific functions in that, but it won't help you to cancel a thread that is stuck in a blocking library/system call.您可以在其中包装标准库或特定于平台的函数,但它不会帮助您取消卡在阻塞库/系统调用中的线程。
There is no standard mechanism to cancel a blocking thread or cancel a thread involuntary.没有取消阻塞线程或非自愿取消线程的标准机制。 But you can access
.native_handle()
of a std::thread
or std::jthread
to get a handle to the underlying platform-specific thread implementation and then use platform-specific features from that (although you probably need to check that they don't interfere with the std::thread
/ std::jthread
implementation; all of this will be implementation-defined).但是您可以访问
std::thread
或std::jthread
的.native_handle()
以获取底层特定于平台的线程实现的句柄,然后从中使用特定于平台的功能(尽管您可能需要检查它们是否不' 干扰std::thread
/ std::jthread
实现;所有这些都将由实现定义)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.