Some example code for that (hanger.c):
Code:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
static int done = 0;
void obnoxious_library_function(void)
{
if (random() % 2)
{
printf( "OK.\n" );
}
else
{
printf( "Hanging...\n" );
sleep( 60 );
}
}
void* my_wrapper(void *genarg)
{
assert( genarg == NULL );
done = 0;
obnoxious_library_function( );
done = 1;
return NULL;
}
int main(void)
{
for (;;)
{
pthread_t thread;
int ret;
ret = pthread_create( &thread, NULL, &my_wrapper, NULL );
assert( ret == 0 );
sleep( 1 );
if (!done)
{
void *result;
ret = pthread_cancel( thread );
assert( ret == 0 );
ret = pthread_join( thread, &result );
assert( result == PTHREAD_CANCELED );
}
}
}
Makefile example:
Code:
CFLAGS=-W -Wall -Werror -pedantic
LDFLAGS=-pthread
CC=gcc
all: hanger
But again: That library function behavior sounds really weird.