Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop a running pthread thread?

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();
            }
        }
    }
}
like image 337
xDianneCodex Avatar asked Sep 16 '13 11:09

xDianneCodex


People also ask

How do I stop a running thread in Linux?

You can use pthread_cancel() to kill a thread: int pthread_cancel(pthread_t thread); Note that the thread might not get a chance to do necessary cleanups, for example, release a lock, free memory and so on..

How do you end all threads in C++?

Master C and Embedded C Programming- Learn as you go The C++11 does not have direct method to terminate the threads. The std::future<void> can be used to the thread, and it should exit when value in future is available.

Does pthread_join wait for thread to finish?

The pthread_join() function waits for the thread specified by thread to terminate. If that thread has already terminated, then pthread_join() returns immediately. The thread specified by thread must be joinable.

Which pthread function should you use to wait for the termination of a thread?

pthread_join: used to wait for the termination of a thread. Syntax: int pthread_join(pthread_t th, void **thread_return);


1 Answers

Based on your code I can give a simple answer:

In this case do not use threads at all.

You do not need them. Store the start time, let the user answer, check the time again after user gives an answer.

{
  time_t startTimeSec = time(NULL);

  // answering

  time_t endTimeSec = time(NULL);
  time_t timeTakenSec = endTime-startTime;
  if (timeTaken > 10) { 
    // do your thing
  }
}

To answer your question:

You should use a mutex-protected or volatile variable to asynchronously communicate between threads. Set that variable from one thread and check it in another. Then reset its value and repeat. A simple snippet:

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

Using pthread_cancel() is an option, but I would not suggest doing it. You will have to check the threads state after this call returns, since pthread_cancel() does not wait for the actual thread stop. And, which to me is even more important, I consider using it ugly.

like image 188
Dariusz Avatar answered Nov 08 '22 21:11

Dariusz