pthread_cancel without detach
pthread_cancel without detach
Dec 17, 2018
If you use "pthread_cancel" without "pthread_detach", the process does not release the thread's stack area.
[pthread_example.c]
//gcc -g -Wall -o pthread_example pthread_example.c -pthread
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pthread.h>
#include <unistd.h>
static char cmd_to_show_vmsize[ 64 ];
void* thread_func( void* args )
{
// pthread_detach( pthread_self() );
sleep( 3600 );
return NULL;
}
int main()
{
sprintf( cmd_to_show_vmsize, "grep VmSize /proc/%d/status", getpid() );
while( 1 )
{
pthread_t thread;
pthread_create( &thread, NULL, thread_func, (void *)NULL );
sleep( 1 );
pthread_cancel( thread );
sleep( 1 );
system( cmd_to_show_vmsize );
}
return 0;
}
Then you can see the vm size continues to increase.
VmSize: 16832 kB
VmSize: 25028 kB
VmSize: 33224 kB
VmSize: 41420 kB
VmSize: 49616 kB
VmSize: 57812 kB
Confirmed version of Linux : CentOS 7.6.1810