久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 C++11 中終止線程?

How do I terminate a thread in C++11?(如何在 C++11 中終止線程?)
本文介紹了如何在 C++11 中終止線程?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我不需要正確終止線程,或者讓它響應終止"命令.我有興趣使用純 C++11 強行終止線程.

I don't need to terminate the thread correctly, or make it respond to a "terminate" command. I am interested in terminating the thread forcefully using pure C++11.

推薦答案

  1. 您可以從任何線程調用 std::terminate() 并且您所指的線程將強制結束.

  1. You could call std::terminate() from any thread and the thread you're referring to will forcefully end.

你可以安排 ~thread() 在目標線程的對象上執行,無需介入 join()detach() 在那個對象上.這將具有與選項 1 相同的效果.

You could arrange for ~thread() to be executed on the object of the target thread, without a intervening join() nor detach() on that object. This will have the same effect as option 1.

你可以設計一個異常,它有一個拋出異常的析構函數.然后安排目標線程在要強行終止的時候拋出這個異常.這個問題的棘手部分是讓目標線程拋出這個異常.

You could design an exception which has a destructor which throws an exception. And then arrange for the target thread to throw this exception when it is to be forcefully terminated. The tricky part on this one is getting the target thread to throw this exception.

選項 1 和 2 不會泄漏進程內資源,但它們會終止每個線程.

Options 1 and 2 don't leak intra-process resources, but they terminate every thread.

選項 3 可能會泄漏資源,但部分合作,因為目標線程必須同意拋出異常.

Option 3 will probably leak resources, but is partially cooperative in that the target thread has to agree to throw the exception.

在 C++11(我知道)中沒有可移植的方式來非合作地殺死多線程程序中的單個線程(即不殺死所有線程).沒有動力設計這樣的功能.

There is no portable way in C++11 (that I'm aware of) to non-cooperatively kill a single thread in a multi-thread program (i.e. without killing all threads). There was no motivation to design such a feature.

一個 std::thread 可能有這個成員函數:

A std::thread may have this member function:

native_handle_type native_handle();

您也許可以使用它來調用依賴于操作系統的函數來執行您想要的操作.例如在 Apple 的操作系統上,這個函數存在并且 native_handle_type 是一個 pthread_t.如果你成功了,你很可能會泄漏資源.

You might be able to use this to call an OS-dependent function to do what you want. For example on Apple's OS's, this function exists and native_handle_type is a pthread_t. If you are successful, you are likely to leak resources.

這篇關于如何在 C++11 中終止線程?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進)
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: a级毛片毛片免费观看久潮喷 | 欧美日韩国产在线观看 | 欧美2区 | 成人在线视频免费看 | 亚洲一区二区三区在线 | 毛片一级黄色 | 精品久久久久久久久久久 | 亚洲人成在线播放 | 免费成人高清在线视频 | 久久中文字幕一区 | 国产精品成av人在线视午夜片 | 中文在线一区二区 | 国产免费观看视频 | 欧美日韩不卡 | 欧美一区二区三区在线视频 | 一区二区在线不卡 | 日韩欧美在线视频 | 欧美性久久久 | 久久人体视频 | 日本一区二区高清不卡 | 五月天国产视频 | 成人在线精品视频 | 亚洲福利在线观看 | 国产sm主人调教女m视频 | 国产乱码精品1区2区3区 | 亚洲国产一区在线 | 午夜欧美一区二区三区在线播放 | 亚洲综合三区 | 国产成人在线播放 | 欧产日产国产精品国产 | 一级做a爰片性色毛片 | 精品国产乱码久久久久久蜜退臀 | 欧美电影免费观看高清 | 亚洲天堂免费 | 日韩一级在线 | 国产免费福利 | 亚洲精品九九 | 在线观看黄免费 | 蜜桃视频在线观看免费视频网站www | 一区欧美 | 日韩欧美在线不卡 |