简体   繁体   中英

C++ pthread - How to cancel a thread?

I have a pthread that I created and now I want that in a specific time interval the thread execute some code. But the user should also be able to cancel the thread. How can I cancel a thread and ensure that the thread is not cancelled when it execute the code?

In Java you handle this with

while(!isInterrupted)

Is there any similar solution with pthreads.

In the Question's example code you are checking some variable. This is not the normal pattern for interrupting threads in Java.

In Java, you interrupt a thread by calling the interrupt() method.

The thread then checks if it is interrupted inside IO and system calls (which can throw InterruptedException when this happens; this means a thread that is sleeping or waiting on IO can be awoken when interrupted) or by sampling the isInterrupted() flag (typically used in a condition in a loop, as in Question).

The distinction is important; checking some flag variable you've declared is only possible in loops and your own code; the Java interrupting system works for all threads and all non-CPU-blocking code without special effort on the part of the programmer.

Pthreads has the pthread_cancel() pattern which works like the Java interrupting pattern.

pthread_cancel is available for sending cancel requests:

A thread's cancellation type, determined by pthread_setcanceltype(3) , may be either asynchronous or deferred (the default for new threads). Asynchronous cancelability means that the thread can be canceled at any time (usually immediately, but the system does not guarantee this). Deferred cancelability means that cancellation will be delayed until the thread next calls a function that is a cancellation point. A list of functions that are or may be cancellation points is provided in pthreads(7) .

A thread's cancelability state, determined by pthread_setcancelstate(3) , can be enabled (the default for new threads) or disabled. If a thread has disabled cancellation, then a cancellation request remains queued until the thread enables cancellation. If a thread has enabled cancellation, then its cancelability type determines when cancellation occurs.

So there are several options:

1: while value checking (works very well, but you don't have much control).

2: check the pthread_cancel manpage, it works to but with strict rules.

3: using pthread_signal, first you need to block, than signal for resume. It has the same issues as the second option.

  • Using pthreads cancel and signal will only work from within the thread that must be locked. So setting a variable to initiate the signal block. Unlocking can be done by any other thread.
  • The same can be done using mutex or semaphores (pthread_mutex, pthread_semaphore).

A site I recommend: http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

There's no specific function to cancel a thread You can use pthread_cancel to cancel the thread, as mentioned (but I would advise against it, unless you know what you're doing), and you have to set up your own timers. But the while(!isInterrupted) is pretty acceptable way of doing it.

It should basically be like this:

while(!isInterrupted)
{
// whatever you want to do
sleep(howLongYouWantToWait);
}
// clean up and exit the thread function here

and in the main thread have a global (or other, see below)

volatile bool isInterrupted = false;

and set it to true when you're done, and pthread_join if you want to wait for the thread to finish.

Instead of global, you can use a class variable, or a flag pointer passed to the thread function, or any other way, global is the simplest and the least preferable.

Of course, if you want to cancel the thread while it waits, and not to have it canceled only after it finishes the whole loop, then you need to deal with signals, and other stuff, but I think you're not looking for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM