Home > Software engineering >  Thread leak detected when using condition variable instead of join() with pthread
Thread leak detected when using condition variable instead of join() with pthread

Time:05-31

I'm new to pthread synchronization, searched "pthread condition variable" in google and grab an example from the pdf: https://pages.cs.wisc.edu/~remzi/OSTEP/threads-cv.pdf .

The example code is as follow, whose purpose is "use condition variable and a variable done to implement pthread_join()" (as I understand):

// https://godbolt.org/z/8rPMq54K8
#include <stdio.h>
#include <pthread.h>

volatile int done  = 0;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t c  = PTHREAD_COND_INITIALIZER;
void thr_exit() {
    pthread_mutex_lock(&m);
    done = 1;
    pthread_cond_signal(&c);
    pthread_mutex_unlock(&m);
}
void *child(void *arg) {
    printf("child\n");
    thr_exit();
    return NULL;
}
void thr_join() {
    pthread_mutex_lock(&m);
    while (done == 0)
    {
        pthread_cond_wait(&c, &m);
    }
    pthread_mutex_unlock(&m);
}

int main(int argc, char *argv[])
{
    printf("parent: begin\n");
    pthread_t p;
    pthread_create(&p, NULL, child, NULL);
    thr_join();
    printf("parent: end\n");
    return 0;
}

Compiled with:

clang   main.cpp -fsanitize=thread -g -fno-omit-frame-pointer

Running it will see "thread leak" reported:

(base) ➜  case12 git:(main) ✗ ./a.out
a.out(71120,0x10433c580) malloc: nano zone abandoned due to inability to preallocate reserved vm space.
parent: begin
child
parent: end
==================
WARNING: ThreadSanitizer: thread leak (pid=71120)
  Thread T1 (tid=1365892, finished) created by main thread at:
    #0 pthread_create <null>:74364228 (libclang_rt.tsan_osx_dynamic.dylib:arm64e 0x2bbe8)
    #1 main main_v4.cpp:31 (a.out:arm64 0x100003e38)

SUMMARY: ThreadSanitizer: thread leak main_v4.cpp:31 in main
==================
ThreadSanitizer: reported 1 warnings

Is this C code really a thread leak, or just a false positive report from tsan?

CodePudding user response:

Is this C code really a thread leak, or just a false positive report from tsan?

It is really a thread leak, arising from the fact that you cannot implement a substitute for pthread_join(). At least, not in any portable way or based only on the C (or C) and pthreads specifications. The program starts a thread and neither detaches nor joins it before terminating. That's a thread leak.

The program does successfully and reliably wait to terminate until the child thread provides evidence that it has called thr_exit(), but that is not a substitute for joining the child thread.

CodePudding user response:

(This is not an answer, just the solution code that I can write, after reading John Bollinger's answer.)

Solution 1:

int main(int argc, char *argv[])
{
    printf("parent: begin\n");
    pthread_t p;
    pthread_create(&p, NULL, child, NULL);
    pthread_detach(p);  // !! new added line
    thr_join();
    printf("parent: end\n");
    return 0;
}

Solution2:

void *child(void *arg) {
    pthread_detach(pthread_self()); //!! new added line
    printf("child\n");
    thr_exit();
    return NULL;
}
  • Related