[英]How to stop a running pthread thread?
如何立即退出或停止線程?
當用戶輸入答案時,如何使其立即停止? 我希望它為每個問題重置。
這是我的代碼,其中涉及線程
int q1() {
int timer_start;
char ans[] = "lol";
char user_ans[50];
timer_start = pthread_create( &xtimer,NULL,(void*)timer_func,(void*)NULL);
printf("What is the capital city of Peru?\n");
while(limit){
scanf("%s",user_ans);
if(limit)
{
if(!strcmp(user_ans, ans))
{
// printf("YAY!\n");
score++;
// q2();
}
else
{
game_over();
}
}
}
}
您可以簡單地在該線程上調用pthread_cancel
以退出它。 您可以通過pthread_kill
發送 SIGSTOP/SIGCONT 信號來停止/重啟它。
但是,如果您只想要一個計時器,為什么必須線程?
根據您的代碼,我可以給出一個簡單的答案:
在這種情況下,根本不要使用線程。
你不需要它們。 存儲開始時間,讓用戶回答,用戶回答后再次查看時間。
{
time_t startTimeSec = time(NULL);
// answering
time_t endTimeSec = time(NULL);
time_t timeTakenSec = endTime-startTime;
if (timeTaken > 10) {
// do your thing
}
}
回答你的問題:
您應該使用互斥保護或 volatile 變量在線程之間進行異步通信。 從一個線程設置該變量並在另一個線程中檢查它。 然后重置其值並重復。 一個簡單的片段:
int stopIssued = 0;
pthread_mutex_t stopMutex;
int getStopIssued(void) {
int ret = 0;
pthread_mutex_lock(&stopMutex);
ret = stopIssued;
pthread_mutex_unlock(&stopMutex);
return ret;
}
void setStopIssued(int val) {
pthread_mutex_lock(&stopMutex);
stopIssued = val;
pthread_mutex_unlock(&stopMutex);
}
使用pthread_cancel()
是一種選擇,但我不建議這樣做。 您必須在此調用返回后檢查線程狀態,因為pthread_cancel()
不會等待實際線程停止。 而且,對我來說更重要的是,我認為使用它很難看。
使用方法來停止線程是一種粗暴的方式。 您應該禮貌地要求線程通過發出信號來停止。 因此,線程將有一個選項來整理自己,例如,如果它已經分配了內存,如果線程被取消,它將沒有任何機會這樣做。
該方法相對簡單,不包括操作系統信令:
在線程外部定義線程狀態變量或結構。 在 pthread_create 處指向它並取消引用線程中的狀態變量。
int thread_state = 0; // 0: normal, -1: stop thread, 1: do something
static void *thread_1 (void *arg)
{
int* pthread_state = arg;
... // initialize the thread locals
while(1)
{
switch( *pthread_state )
{
case 0: // normal thread loop
...
break;
case -1:
... // tidy or whatever is necessary
pthread_exit(0); // exit the thread signalling normal return
break;
case 1: //
... // do something special
break;
}
}
}
pthread_create (&t_1, NULL, thread_1, (void*)&thread_state);
...
thread_state = -1; // signal to the thread to stop
// maybe use pthread_exit(0) to exit main.
// this will leave the threads running until they have finished tidy etc.
甚至可以使用結構與線程進行通信,前提是它是簡單的“原子”變量或建立了簡單的握手機制。 否則可能需要使用互斥鎖。 使用 pthread_join 等待線程終止。
@Naruil 建議調用pthread_cancel()幾乎是我找到的最佳解決方案,但如果您不執行以下操作,它將無法工作。
根據pthread_cancel的手冊頁, pthread_cancelibility 取決於兩件事
thread_cancel_state默認是PTHREAD_CANCEL_ENABLE ,所以我們主要關心的是thread_cancel_type ,它的默認值是類型PTHREAD_CANCEL_DEFFERED但我們需要在該線程上設置PTHREAD_CANCEL_ASYNCHRONOUS ,我們不想取消。
下面給出一個例子:
#include <stdio.h>
#include <pthread.h>
void *thread_runner(void* arg)
{
//catch the pthread_object as argument
pthread_t obj = *((pthread_t*)arg);
//ENABLING THE CANCEL FUNCTIONALITY
int prevType;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &prevType);
int i=0;
for( ; i < 11 ; i++)//1 - > 10
{
if(i == 5)
pthread_cancel(obj);
else
printf("count -- %d", i);
}
printf("done");
}
int main(int argc, char *argv[])
{
pthread_t obj;
pthread_create(&obj, NULL, thread_runner, (void*)&obj);
pthread_join(obj, NULL);
return 0;
}
使用gcc filename.c -lpthread運行它並輸出以下內容: count -- 0 count -- 1 count -- 2 count -- 3 count -- 4
請注意, done永遠不會打印,因為當 i 變為 5 並且正在運行的線程被取消時線程被取消。 特別感謝@Naruil 的“pthread_cancel”建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.