如何停止正在运行的pthread线程? [英] How to stop a running pthread thread?

查看:434
本文介绍了如何停止正在运行的pthread线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能退出或立即停止线程?

我怎样才能使它立即停止当用户输入一个答案?
我希望它重置每一个问题。

下面是我的code,其中线程参与

  INT Q1(){
    INT timer_start;
    CHAR ANS [] =笑;
    焦炭user_ans [50];
    timer_start =在pthread_create(安培; xtimer,NULL,(void *的)timer_func,(无效*)NULL);
    的printf(什么是秘鲁的首都\\ n吗?);    而(限制){
        scanf函数(%S,user_ans);
        如果(限制)
        {
             如果(!STRCMP(user_ans,ANS))
              {               //输出(YAY \\ n!);
                得分++;
               // Q2();            }
            其他
            {
                游戏结束();
            }
        }
    }
}


解决方案

您可以简单地调用 pthread_cancel可以在该线程退出。并且你可以通过 pthread_kill 发送SIGSTOP / SIGCONT信号来停止/重新启动它。


但是,如果你想要的是一个计时器,你为什么一定要拧?

How can I exit or stop a thread immediately?

How can I make it stop immediately when the user enters an answer? I want it to reset for every question.

Here's my code where threading is involved

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();
            }
        }
    }
}

解决方案

You can simply call pthread_cancel on that thread to exit it. And you can send SIGSTOP/SIGCONT signal via pthread_kill to stop/restart it.


But if all you want is a timer, why must you thread?

这篇关于如何停止正在运行的pthread线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆