I have the following code. the build application is myprogram.
If I launch myprogram and then killall myprogram and immediately after that I launch again myprogram then myprogram crash.
the crash cause is due to that the management thread created by the first launch is not properly cleared before the second launch.
so in the second launch whent myprogram try to create thread with the pthread and the old thread management is not removed yet so it causes a crash.
Are there a way to kill the management thread at the end of my first launch or at the beginning of my second launch with C?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_t test_thread;
void *thread_test_run (void *v)
{
int i=1;
while(1)
{
printf("into thread %d\r\n",i);
i++;
sleep(1);
}
return NULL
}
int main()
{
// ps aux | grep myprogram ---> show 1 myprogram (1 for the main application)
pthread_create(&test_thread, NULL, &thread_test_run, NULL);
// ps aux | grep myprogram ---> show 3 myprogram
// (1st for the main application)
// (2nd for the management thread. thread which manage all created thread)
// (3rd for the created thread)
sleep (20);
pthread_cancel(test_thread);
// ps aux | grep myprogram ---> show 2 myprogram and
// (1st for the main application)
// (2nd for the management thread. thread which manage all created thread)
sleep(100);
// in this period (before the finish of myprogram)
// I execute killall to kill myprogram
// and then immediately I re-launch myprogram and then the program crash
// because the management thread is not immediately killed
}
BTW:
the linux use libuClibc-0.9.30.1.so
and according to this question How to kill all subprocess created with pthread_create after cancelling a thread? this libc use linux thread implementation of pthread
and does not use libc with NPTL ("Native posix thread library") implementation
so the management thread will be created only for this case of libc.