Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to forcibly terminate C++ multithread

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about how C++ multithreading carries out compulsory termination, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Foreword:

The cause of the story comes from the fact that when I optimize other people's C++ source code, I want to improve the operational efficiency of the program in a multi-threaded way, there are mainly the following requirements and difficulties:

Multiple threads run in parallel to see which model runs fast and ends other threads after running out. There is no communication process between threads.

The source code model is very complex, there are many function calls, and it is difficult to change, so it is not suitable for communication termination through signals or flags.

There are several ways in which threads end:

Return return of the thread function (recommended): this is the safest way to exit the thread. After the thread function return returns, it cleans up the class objects applied in the function, that is, calls the destructors of those objects. The _ endthreadex () function is then automatically called to clean up the resources requested by the _ beginthreadex () function (mainly the tiddata object created).

Threads in the same process or in another process call the TerminateThread function (this method should be avoided): TerminateThread can undo any thread, where the hThread parameter is used to identify the handle of the thread that was terminated. When a thread stops running, its exit code becomes the value you pass as the dwExitCode parameter. At the same time, the usage count of kernel objects for threads is decremented. Note that the TerminateThread function is a function that runs asynchronously, that is, it tells the system that you want the thread to terminate, but when the function returns, there is no guarantee that the thread will be undone. If you need to know exactly that the thread has stopped running, you must call WaitForSingleObject or similar function to pass the handle to the thread.

By calling the ExitThread function: the thread will undo itself (it's best not to use this method). This function terminates the running of the thread and causes the operating system to clear all operating system resources used by the thread. However, C++ resources, such as C++ objects, will not be destructed.

The ExitProcess and TerminateProcess functions can also be used to terminate the running of a thread (this method should be avoided):

Options 2 and 3 can cause memory leaks. In fact, no language or operating system can provide you with the convenience of abruptly terminating threads asynchronously without warning you not to use them. All of these execution environments strongly recommend that developers even require that multithreaded applications be built on the basis of collaborative or synchronous thread termination.

The existing thread end functions, including pthread_exit () and pthread_cancel () in pthread.h of linux system, ExitThread () and TerminateThread () in win32.h of windows system, that is, C++ does not provide the ability to kill a thread and can only passively wait for the natural end of a thread, the destructor ~ thread () can not stop the thread, the destructor can only terminate thread joinable when the thread is at rest, for connected / detached threads The destructor simply cannot terminate the thread.

To terminate the thread of an OS / compiler-related function, we need to know how to get the native thread data type std::thread from C++. Fortunately, std::thread provides an API native_handle () to get the native handle type of the thread before or before the call. And you can pass this local handle to the local OS thread termination function, such as join () detach () pthread_cancel ().

The following code is used to display std::thread::native_handle (), std::thread::get_id (), and pthread_self () to return the same code pthread_t to handle Linux / GCC's C++ thread

# include # include std::mutex iomutex;void f (int num) {std::this_thread::sleep_for (std::chrono::seconds (1)); std::lock_guard lk (iomutex); std::cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report