是否有可能杀死旋转的线程?

时间:2011-11-02 10:14:16

标签: c++ multithreading

我使用ZThreads来说明问题,但我的问题适用于C ++中的PThreads,Boost Threads和其他类似的线程库。

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

      }
   }
}

我现在按如下方式启动:

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

现在可以杀死(如果必要的话)暴力吗?我可以肯定这样做而不是无限循环我有一个Thread::Sleep(100000),如果它是阻塞的话。但我可以杀死旋转的线程(进行计算)。如果有,怎么样?如果没有,为什么不呢?

9 个答案:

答案 0 :(得分:5)

就Windows而言(来自MSDN):

  

TerminateThread是一个危险的函数,只能用于   最极端的情况。只有你这样才能调用TerminateThread   确切知道目标线程正在做什么,并控制所有   目标线程当时可能正在运行的代码   终止。例如,TerminateThread可以导致   以下问题:

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's 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肯定没有线程杀死功能。

答案 1 :(得分:3)

发布的问题类型的一般解决方案可以在Herb Sutter文章中找到: Prefer Using Active Objects Instead of Naked Threads

这允许你有这样的东西(摘自文章):

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
  }

在活动对象析构函数中,您可以拥有:

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

此解决方案提升了一个干净的线程函数,并避免了与不正常的线程终止相关的所有其他问题。

答案 2 :(得分:1)

可以强制终止线程,但执行此操作的调用将是特定于平台的。例如,在Windows下,您可以使用TerminateThread函数执行此操作。

请记住,如果您使用TerminateThread,则在程序终止之前,线程将无法释放它正在使用的任何资源。

答案 3 :(得分:1)

如果你需要杀死一个线程,请考虑使用一个进程。

特别是如果你告诉我们你的“线程”是一个while(true)循环,它可能会长时间休眠,执行必然阻塞的操作。对我而言,这表明了类似过程的行为。

流程几乎可以在任何时候以各种方式终止,并且始终以干净的方式终止。它们还可以在发生碰撞时提供更高的可靠性。

现代操作系统提供了一系列进程间通信功能:套接字,管道,共享内存,内存映射文件......它们甚至可以交换文件描述符。

好的操作系统具有写时复制机制,因此fork的流程很便宜。

请注意,如果您的操作可以以非阻塞方式进行,那么您应该使用类似轮询的机制。 Boost :: asio可以帮助那里。

答案 4 :(得分:0)

您可以使用TerminateThread()API,但不建议这样做。

更多详情: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686717(v=vs.85).aspx

答案 5 :(得分:0)

正如人们已经说过的那样,没有可移植的方法来杀死一个线程,在某些情况下根本不可能。如果您可以控制代码(即可以修改它),最简单的方法之一是使用一个布尔变量,线程以固定间隔检查,如果设置,则尽快终止该线程。

答案 6 :(得分:0)

你不能添加如下的内容

do {

  //stuff here

} while (!abort)

如果它们很小且不太长(如上面的循环中)或在中间并且如果它很长则中止计算,那么在计算之间偶尔检查一次标志?

答案 7 :(得分:0)

不确定其他库但在pthread库中pthread_kill函数是否可用pthread_kill

答案 8 :(得分:0)

是,

int变量定义为keepAlive=1。 最初设置class MyClass: public Runnable { public: void run() { while(keepAlive) { } } }

的值
keepAlive=0

现在,当您想要杀死线程时,只需设置Terminate的值。

即可。这是如何工作的?

<强> A 即可。线程将一直有效,直到函数执行连续。因此0函数非常简单。将变量值设置为This is the safest way I found till date&amp;它打破了导致杀死线程。 [.class { animation-name: out; animation-duration: 2s; /* Safari and Chrome: */ -webkit-animation-name: out; -webkit-animation-duration: 2s; } .class:hover { animation-name: in; animation-duration: 5s; animation-iteration-count: infinite; animation-direction: normal; /* Safari and Chrome: */ -webkit-animation-name: in; -webkit-animation-duration: 5s; -webkit-animation-iteration-count: infinite; -webkit-animation-direction: alternate; } @keyframes in { from { transform: rotate(50deg); } to { transform: rotate(360deg); } } @-webkit-keyframes in /* Safari and Chrome */ { from { transform: rotate(50deg); } to { -webkit-transform: rotate(360deg); } } @keyframes out { from { transform: rotate(360deg); } to { transform: rotate(0deg); } } @-webkit-keyframes out /* Safari and Chrome */ { from { transform: rotate(360deg); } to { -webkit-transform: rotate(0deg); } }]。