C++ pthread - How to cancel a thread?

10,110

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
10,110
Tobi Weißhaar
Author by

Tobi Weißhaar

Updated on June 04, 2022

Comments

  • Tobi Weißhaar
    Tobi Weißhaar almost 2 years

    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.