From: EML <sa2...@cy...> - 2018-06-28 13:50:27
|
The program below creates two threads, in a detached state, and waits for them to complete with 'pthread_exit'. Compile and run as: > gcc -lpthread test.c > valgrind --leak-check=full a.out valgrind-3.13.0 (compiled from source) reports no errors on some runs, and a possible leak on other runs (560 bytes in 1 blocks), fairly randomly. On my system, the ratio of error-free to bad runs is about 50/50. Is this is a problem with valgrind, or my use of pthreads? *Note that this isn't the standard 'pthread_create' memory leak with joinable threads which haven't been joined, since the threads are detached.* I'm compiling on Centos 6.9, gcc 4.4.7. Thanks. ------------------------------------------------------------------------ #include <stdlib.h> #include <stdio.h> #include <pthread.h> void *thread(void *data) { printf("in new thread %ld\n", (long)data); } int main() { pthread_t thread_id[2]; pthread_attr_t thread_attr; int failed = 1; do { if(pthread_attr_init(&thread_attr)) break; if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED)) break; if(pthread_create(&thread_id[0], &thread_attr, thread, (void*)0)) break; if(pthread_create(&thread_id[1], &thread_attr, thread, (void*)1)) break; failed = 0; } while(0); if(failed) { printf("pthreads call failed\n"); exit(1); } pthread_exit(0); } ------------------------------------------------------------------------ |