C-Thread-Pool/tests/ext_bugs/memleak.c

39 lines
960 B
C
Raw Permalink Normal View History

2015-01-06 21:49:58 +03:00
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
2015-01-06 21:55:49 +03:00
/* This showcasts this issue: https://sourceware.org/ml/glibc-bugs/2007-04/msg00036.html */
2015-01-16 21:21:34 +03:00
/* Also here: http://stackoverflow.com/questions/27803819/pthreads-leak-memory-even-if-used-correctly/27804629 */
2015-01-06 21:55:49 +03:00
2015-01-06 21:49:58 +03:00
volatile int threads_keepalive = 1;
void* thread_do(void *arg){
while(threads_keepalive)
sleep(1);
pthread_exit(NULL);
}
int main(void){
/* Make threads */
pthread_t* threads;
threads = malloc(2 * sizeof(pthread_t));
pthread_create(&threads[0], NULL, &thread_do, NULL);
pthread_create(&threads[1], NULL, &thread_do, NULL);
pthread_detach(threads[0]);
pthread_detach(threads[1]);
sleep(1); // MAKING SURE THREADS HAVE INITIALIZED
/* Kill threads */
threads_keepalive = 0;
sleep(3); // MAKING SURE THREADS HAVE UNBLOCKED
pthread_join(threads[0], NULL);
pthread_join(threads[1], NULL);
free(threads);
return 0;
}