In a multithreaded application I need to cancel threads. Some of them use the flockfile
mechanism which apparently is not a valid cancellation point for pthread_cancel. I managed to kill the threads by sending a SIGUSR1 signal using pthread_kill, forcing them to pthread_exit in the signal handler. Cleaning up the allocated memory proved to be simple in my case (it’s all on the stack), but I can’t, of course, close the file streams. Is there any way of freeing the buffers or removing the locks? On my system each of the unclosed buffers hogs 16KB of memory which accumulates quickly.
In a multithreaded application I need to cancel threads. Some of them use the
Share
Instead of force-killing the threads, you should communicate them a “finish” message, then the thread can clean up after itself before exiting clean.
You are already doing the messageing part with SIGUSR1, so you will need to kepp track of open handles so you can unlock/free any in-use ones from the signal handler