Using pthread_cond_timedwait () to cancel a lengthy task

I have a situation where I would like to cancel a thread if it requires too much to complete. To do this, I use the second thread, which waits for the completion of the first thread, but not more than a few seconds. The pthread_cond_timedwait () function seems to be ideal for my use case, however, it does not seem to behave as I expected. More specifically, even though the pthread_cond_timedwait () function returns ETIMEDOUT, it only does this after the thread that was supposed to cancel completion, which is detrimental to the whole target.

This is my test code:

    #include <unistd.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <iostream>
    #include <cstring>

    #define WAIT_INTERVAL 5
    #define THREAD_SLEEP 10

    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t condition = PTHREAD_COND_INITIALIZER;

    pthread_t t1;
    pthread_t t2;

    void* f1(void*);
    void* f2(void*);

    int main()
    {
        pthread_create(&t1, NULL, &f1, NULL);
        pthread_create(&t2, NULL, &f2, NULL);

        pthread_join(t1, NULL);
        pthread_join(t2, NULL);

        std::cout << "Thread(s) successfully finished" << std::endl << std::flush;

        exit(EXIT_SUCCESS);
    }

    void* f1(void*)
    {
        pthread_mutex_lock(&mutex);
        timespec ts = {0};
        clock_gettime(CLOCK_REALTIME, &ts);
        ts.tv_sec += WAIT_INTERVAL;
        std::cout << __FUNCTION__ << ": Waiting for at most " << WAIT_INTERVAL << " seconds starting now" << std::endl << std::flush;
        int waitResult = pthread_cond_timedwait(&condition, &mutex, &ts);
        if (waitResult == ETIMEDOUT)
        {
            std::cout << __FUNCTION__ << ": Timed out" << std::endl << std::flush;
            int cancelResult = pthread_cancel(t2);
            if (cancelResult)
            {
                std::cout << __FUNCTION__ << ": Could not cancel T2 : " << strerror(cancelResult) << std::endl << std::flush;
            }
            else
            {
                std::cout << __FUNCTION__ << ": Cancelled T2" << std::endl << std::flush;
            }
        }
        std::cout << __FUNCTION__ << ": Finished waiting with code " << waitResult << std::endl << std::flush;
        pthread_mutex_unlock(&mutex);
    }

    void* f2(void*)
    {
        pthread_mutex_lock(&mutex);
        std::cout << __FUNCTION__ << ": Started simulating lengthy operation for " << THREAD_SLEEP << " seconds" << std::endl << std::flush;
        sleep(THREAD_SLEEP);
        std::cout << __FUNCTION__ << ": Finished simulation, signaling the condition variable" << std::endl << std::flush;
        pthread_cond_signal(&condition);
        pthread_mutex_unlock(&mutex);
    }

The result obtained from the above code is as follows:

    f1: Waiting for at most 5 seconds starting now
    f2: Started simulating lengthy operation for 10 seconds
    f2: Finished simulation, signaling the condition variable
    f1: Timed out
    f1: Could not cancel T2 : No such process
    f1: Finished waiting with code 110
    Thread(s) successfully finished

, POSIX, , -, .

, , , .

, , , pthread_cond_timedwait, .

POSIX CentOS 6.5. : 2.6.32-431.5.1.el6.centos.plus.x86_64 # 1 SMP x86_64 x86_64 x86_64 GNU/Linux g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4)

: g++ -o executable_binary -pthread -lrt source_code.cpp

+4
2

: pthread_cond_timedwait, , , , , "", .

, , pthread_cond_timedwait(). , , .

+4

 pthread_create(&t1, NULL, &f1, NULL);
 pthread_create(&t2, NULL, &f2, NULL);

, , thread t1 t2: t1 , pthread_cancel(t2) .

t2, . , , t1 t2, , :)

, :)

+1

Source: https://habr.com/ru/post/1533572/


All Articles