cpthreads

How does printf() works as a safe cancel point? What is it depends on?


I have this little program that is supposed to create a thread and cancel it.

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *thread_function(void *arg)
{
    while(1)
    {
        printf("hello \n"); 
        //sleep(1);
    }
    return NULL;
}

int main()
{
    pthread_t thread;

    if(pthread_create(&thread, NULL, thread_function, NULL) != 0)
    {
        perror("Failed to create a thread");
        return -1;
    }

    printf("After sleep in main thread\n");
    if (pthread_cancel(thread) != 0) {
        perror("Failed to cancel thread");
        return EXIT_FAILURE;
    }


    pthread_join(thread, NULL);
    printf("Thread is cancelled\n");
}

If I use sleep() in thread function, created thread is canceled almost immediately, but if I use printf(), it takes some time to cancel it. In man pthreads I read that sleep() is a safe cancel point while printf() may be a safe cancel point.

How does it work? I mean in what period of time that programme works printf() turns into a cancel point and my thread is being canceled?

And also if I run the programm (also using printf() in thread function) with time command, it can show that programm worked for 2 sec but in reality it worked longer. why?

PS: sorry for the mistakes, English is not my first language.


Solution

  • If I use sleep() in thread function, created thread is canceled almost immediately, but if I use printf(), it takes some time to cancel it. In man pthreads I read that sleep() is a safe cancel point while printf() may be a safe cancel point.

    Perhaps this is a language issue, but "safe" is not an appropriate description. I think what you're trying to get at is that POSIX requires sleep() to be a cancellation point, whereas it merely allows printf() to be one. Of course, that means it also allows printf() to not be one.

    How does it work? I mean in what period of time that programme works printf() turns into a cancel point and my thread is being canceled?

    A function does not "turn into" a cancellation point. Either it is one or it isn't, depending on your system.

    If a function is a cancellation point, and it is called in a thread that has cancellation type "deferred", at a time when that thread has a pending cancellation request, then the thread is cancelled when it makes the call.

    POSIX does not require an attempt to join a thread to complete in any particular amount of time after the target thread is canceled, nor does it require the time from cancellation to join to be consistent with use of different cancellation points, nor with anything else, really.

    HOWEVER, I speculate that you may be misinterpreting your observations, especially in light of:

    if I run the programm (also using printf() in thread function) with time command, it can show that programm worked for 2 sec but in reality it worked longer.

    You are running printf() in a tight loop, which will produce a lot of output, and output to the terminal can be surprisingly slow. It may be that the thread cancellation itself happens fast for your printf(), but output keeps being produced for longer. You should trust time's measurement of the process's timings.