Is it possible to kill a spinning thread?

I use ZThreads to illustrate the question, but my question relates to PThreads, Boost Threads, and other similar thread libraries in C ++.

class MyClass: public Runnable { public: void run() { while(1) { } } } 

Now I run it as follows:

 MyClass *myClass = new MyClass(); Thread t1(myClass); 

Now you can kill (if necessary by force) this thread? I can do this for sure, instead of an infinite loop, I had Thread::Sleep(100000) , that is, if it blocks. But can I kill a spinning stream (do the calculations). If so, how? If not, why not?

+3
source share
10 answers

Regarding Windows (from MSDN ):

TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all the code that the target thread can be running at the time of termination. For example, TerminateThread can cause the following problems:

 If the target thread owns a critical section, the critical section will not be released. If the target thread is allocating memory from the heap, the heap lock will not be released. If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread process could be inconsistent. If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL. 

Boost, of course, does not have the function of destroying the thread.

+5
source

A general solution to the issue in question can be found in Herb Sutter's article: Prefer to use active objects instead of a bare topic

This allows you to have something like this (excerpt from the article):

 class Active { public: typedef function<void()> Message; private: Active( const Active& ); // no copying void operator=( const Active& ); // no copying bool done; // le flag message_queue<Message> mq; // le queue unique_ptr<thread> thd; // le thread void Run() { while( !done ) { Message msg = mq.receive(); msg(); // execute message } // note: last message sets done to true } 

In the destructor of the active object, you can:

 ~Active() { Send( [&]{ done = true; } ); ; thd->join(); } 

This solution contributes to the existence of a pure stream function and eliminates all other problems associated with terminating an unclean stream.

+3
source

You can force a thread to terminate, but a call to do so will be platform specific. For example, on Windows, you can do this using the TerminateThread function.

Keep in mind that if you use TerminateThread, the thread will not be able to release any resources that it uses until the program terminates.

+1
source

If you need to kill a thread, consider using a process.

Especially if you tell us that your "thread" is a while (true) loop that can sleep for a long time, performing operations that are necessarily blocked. For me, this indicates the behavior of the process.

Processes can be terminated in a variety of ways at almost any time and always clean. They can also provide greater reliability in the event of a failure.

Modern operating systems offer many interprocess communication tools: sockets, channels, shared memory, memory mapped files ... They can even exchange file descriptors.

Good OSs have a write-to-write mechanism, so processes are cheap for fork.

Please note that if your operations can be performed in a non-blocking way, then you should use a poll-like mechanism. Boost :: asio can help there.

+1
source

You can use the TerminateThread () API, but this is not recommended.

More details: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686717(v=vs.85).aspx

0
source

As people have already said, there is no portable way to kill a thread, and in some cases it’s not possible at all. If you have control over the code (i.e., it can change it), one of the easiest ways is to have a boolean variable that the thread checks at regular intervals, and if specified, terminate the stream as soon as possible.

0
source

Can't you add something like below

 do { //stuff here } while (!abort) 

And check the flag once in the interval between calculations if they are small and not too long (as in the loop above) or in the middle, and interrupt the calculation if it is long?

0
source

Not sure about other libraries, but the pthread_kill pthread_kill function is available in the pthread library

0
source

Boost.Threads has a breakpoint concept.

For instance:

 void run() { try { lock lk(monitor); while(1) { some_condition.wait(lk); // a boost::condition // Do some work } } catch(thread_interrupted const&) { // Something } } 

From their documentation:

"A running thread can be interrupted by calling the interrupt () member function of the corresponding boost::thread object. When the interrupted thread executes one of the specified breakpoints (or if it is currently blocked during its execution), the interrupt is turned on, then boost::thread_interrupted thrown boost::thread_interrupted will be selected in the interrupted thread. If it is not caught, this will end the execution of the intermittent thread. As with any other exception, the stack will be unwound, and destructors for objects will be executed automatically storage.

When you call obj.interrupt (), it throws excepion the next time you call .wait (lk);

0
source

Yes,

Define the keepAlive variable as int . Set the value keepAlive=1 .

 class MyClass: public Runnable { public: void run() { while(keepAlive) { } } } 

Now that everyone who wants to kill the thread just sets the value keepAlive=0 .

Q How it works?

A. The thread will be active until the function is continuous. So this is a pretty simple Terminate function. set the value of the variable to 0 and it will break, which will kill the thread. [ This is the safest way I found till date ].

0
source

Source: https://habr.com/ru/post/974789/


All Articles