簡體   English   中英

如何殺死或終止增強線程

[英]How to kill or Terminate a boost Thread

我想終止或終止加速線程。 代碼在這里:

DWORD WINAPI  StartFaceDetector(LPVOID temp)
{   
    int j=0;
    char **argv1;
    QApplication a(j,argv1);//add some thread here  
    gui::VisualControl w;
    t=&w;
    boost::thread u(&faceThread);       
    w.show();
    a.exec();
    // I Want to close u thread here.   
    return 0;   
}

我想在函數返回之前關閉該升壓線程。 提前致謝。

在Windows上:

TerminateThread(u.native_handle(), 0);

在Linux / QNX / UNIX /具有pthread支持的任何平台上:

pthread_cancel(u.native_handle());

要么

pthread_kill(u.native_handle(), 9);

請注意,由於行為是依賴於平台且定義不明確的,因此boost的作者有意忽略了這一點。 但是,您並不是唯一可以使用此功能的人...

使用interrupt() 另外,您應該定義中斷點 一旦到達中斷點之一,線程將在調用interrupt()之后被中斷。

u.interrupt();

更多信息

調用interrupt()只是在該線程的線程管理結構中設置一個標志並返回:它不等待該線程實際被中斷。 這很重要,因為線程只能在預定義的中斷點之一處被中斷,並且可能是線程從不執行中斷點,因此從不看到請求。 當前,中斷點為:

  • boost::thread::join()
  • boost::thread::timed_join()
  • boost::condition_variable::wait()
  • boost::condition_variable::timed_wait()
  • boost::condition_variable_any::wait()
  • boost::condition_variable_any::timed_wait()
  • boost::this_thread::sleep()
  • boost::this_thread::interruption_point()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM