Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancelling a thread using pthread_cancel : good practice or bad

I have a C++ program on Linux (CentOS 5.3) spawning multiple threads which are in an infinite loop to perform a job and sleep for certain minutes. Now I have to cancel the running threads in case a new configuration notification comes in and freshly start new set of threads, for which i have used pthread_cancel. What I observed was, the threads were not getting stopped even after receiving cancel indication,even some sleeping threads were coming up after the sleep was completed.

As the behavior was not desired, usage of pthread_cancel in the mentioned scenario raises question about being good or bad practice.

Please comment on the pthread_cancel usage in above mentioned scenario.

like image 655
Mandar Avatar asked Jan 21 '11 15:01

Mandar


People also ask

What happens when a thread is Cancelled?

Thread cancellation lets a thread terminate the execution of any other thread in the process. When a cancellation requested is acted on, the target thread (the thread being cancelled) is allowed to defer cancellation requests and to perform application-specific cleanup processing.

Can you cancel a thread?

Cancellation allows a thread to request the termination of any other thread in the process. Cancellation is an option when all further operations of a related set of threads are undesirable or unnecessary.


3 Answers

In general thread cancellation is not a really good idea. It is better, whenever possible, to have a shared flag, that is used by the threads to break out of the loop. That way, you will let the threads perform any cleanup they might need to do before actually exiting.

On the issue of the threads not actually cancelling, the POSIX specification determines a set of cancellation points ( man 7 pthreads ). Threads can be cancelled only at those points. If your infinite loop does not contain a cancellation point you can add one by calling pthread_testcancel. If pthread_cancel has been called, then it will be acted upon at this point.

like image 64
David Rodríguez - dribeas Avatar answered Oct 14 '22 11:10

David Rodríguez - dribeas


If you are writing exception safe C++ code (see http://www.boost.org/community/exception_safety.html) than your code is naturally ready for thread cancellation. glibs throws C++ exception on thread cancel, so that your destructors can do the appropriate clean-up.

like image 24
Maxim Egorushkin Avatar answered Oct 14 '22 12:10

Maxim Egorushkin


You can do the equivalent of the code below.

#include <pthread.h>
#include <cxxabi.h>
#include <unistd.h>
...
void *Control(void* pparam)
{
    try
    {
        // do your work here, maybe long loop
    }   
    catch (abi::__forced_unwind&)
    {  // handle pthread_cancel stack unwinding exception
        throw;
    }
    catch (exception &ex) 
    {
        throw ex;
    }
}

int main()
{
    pthread_t tid;
    int rtn;
    rtn = pthread_create( &tid, NULL, Control, NULL );

    usleep(500);
    // some other work here

    rtn = pthtead_cancel( tid );
}
like image 38
Jack Avatar answered Oct 14 '22 11:10

Jack