Search code examples
cmultithreadingpthreads

C pthreads - Why is `sleep` interrupted by a thread cancellation request despite cancel state being disabled?


I'm currently learning C and POSIX APIs, in particular pthreads. I've encountered following situation which surprised me and seemed like it isn't supposed to be that way. The sleep call inside a thread is being interrupted by a cancellation request from the main thread, despite cancel state set to disabled in that thread.

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <assert.h>

static void *thread(void *arg)
{
    int oldstate;

    printf("other thread: started\n");

    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
    printf("other thread: started doing thing that can't be canceled\n");

    errno = 0;
    sleep(10);
    if (errno)
        perror("sleep");

    printf("other thread: finished doing thing that can't be canceled\n");
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);

    printf("other thread: exiting\n");

    return NULL;
}

int main(void)
{
    pthread_t thr;
    void *res;

    pthread_create(&thr, NULL, thread, NULL);
    printf("main: created other thread\n");

    printf("main: letting other thread work for 3 seconds...\n");
    sleep(3);
    printf("main: canceling other thread\n");
    pthread_cancel(thr);

    printf("main: joining with other thread\n");
    res = NULL;
    pthread_join(thr, &res);
    if (res == PTHREAD_CANCELED)
        printf("main: canceled other thread\n");
    else
        printf("main: other thread result: %p\n", res);

    printf("main: exiting\n");

    return 0;
}

I expected the following output from this program:

main: created other thread
main: letting other thread work for 3 seconds...
other thread: started
other thread: started doing thing that can't be canceled
main: canceling other thread
main: joining with other thread
other thread: finished doing thing that can't be canceled
other thread: exiting
main: canceled other thread
main: exiting

However, sleep gets interrupted and the output is as follows:

main: created other thread
main: letting other thread work for 3 seconds...
other thread: started
other thread: started doing thing that can't be canceled
main: canceling other thread
main: joining with other thread
sleep: Interrupted system call
other thread: finished doing thing that can't be canceled
other thread: exiting
main: canceled other thread
main: exiting

This just seems wrong. Cancellation request is not a signal, right? Then why does it cause sleep to be interrupted? I expected the code to execute as if instead of just sleep(10) there was a following code:

// before `thread`:
static pthread_mutex_t sleep_mut = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t sleep_cv = PTHREAD_COND_INITIALIZER;

static void *sleep_thread(void *arg)
{
    int sleep_for = *((int *) arg);

    sleep(sleep_for);
    pthread_cond_broadcast(&sleep_cv);

    return NULL;
}

static void *thread(void *arg)
{
    // ...
    // instead of `sleep`:
    pthread_mutex_lock(&sleep_mut);
    pthread_t sleep_thr;
    int sleep_for = 10;
    pthread_create(&sleep_thr, NULL, sleep_thread, &sleep_for);
    pthread_cond_wait(&sleep_cv, &sleep_mut);
    pthread_mutex_unlock(&sleep_mut);
    // ...
}


Solution

  • As I was pointed out in the comments, Linux actually does implement cancellation using a signal. That explains everything.