Is it possible to kill a spinning thread?

2020-04-11 02:14发布

I am using ZThreads to illustrate the question but my question applies to PThreads, Boost Threads and other such threading libraries in C++.

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

      }
   }
}

I now launch this as follows:

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

Is it now possible to kill (violently if necessary) this thread? I can do this for sure instead of the infinite loop I had a Thread::Sleep(100000) that is, if it is blocking. But can I kill a spinning thread (doing computation). If yes, how? If not, why not?

9条回答
贪生不怕死
2楼-- · 2020-04-11 02:18

Can't you do add something like below

do {

  //stuff here

} while (!abort)

And check the flag once in a while between computations if they are small and not too long (as in the loop above) or in the middle and abort the computation if it is long?

查看更多
淡お忘
3楼-- · 2020-04-11 02:20

A general solution to the kind of question posted can be found in Herb Sutter article: Prefer Using Active Objects Instead of Naked Threads

This permits you to have something like this (excerpt from 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 active object destructor you can have then:

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

This solution promotes a clean thread function exist, and avoids all other issues related to an unclean thread termination.

查看更多
我想做一个坏孩纸
4楼-- · 2020-04-11 02:28

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

查看更多
劫难
5楼-- · 2020-04-11 02:31

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

Especially if you tell us that your "thread" is a while (true) loop that may sleep for a long period of time performing operations that are necessarily blocking. To me, that indicate a process-like behavior.

Processes can be terminated in a various number of ways at almost any time and always in a clean way. They may also offer more reliability in case of a crash.

Modern operating systems offer an array of interprocess communications facilities: sockets, pipes, shared memory, memory mapped files ... They may even exchange file descriptors.

Good OSes have copy-on-write mechanism, so processes are cheap to fork.

Note that if your operations can be made in a non-blocking way, then you should use a poll-like mechanism instead. Boost::asio may help there.

查看更多
小情绪 Triste *
6楼-- · 2020-04-11 02:33

It is possible to terminate a thread forcefully, but the call to do it is going to be platform specific. For example, under Windows you could do it with the TerminateThread function.

Keep in mind that if you use TerminateThread, the thread will not get a chance to release any resources it is using until the program terminates.

查看更多
We Are One
7楼-- · 2020-04-11 02:35

You can with TerminateThread() API, but it is not recommended.

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

查看更多
登录 后发表回答