#include #include #include #define LOOPS 1024 #define WASTE_TIME (1 << 18) typedef void *(*create_func)(void *); typedef void (*cleanup_func)(void *); void cleanup_function() { puts("Ladies and gentlemen, Elvis has left the building."); } void *child_function() { int i; pthread_cleanup_push((cleanup_func)cleanup_function, NULL); for (i = 0; i < LOOPS; i++) { printf("And a %d,\n", i); pthread_testcancel(); } pthread_cleanup_pop(0); puts("Hit it!"); return NULL; } int main(int argc, char *argv[]) { int sum, i; pthread_t child_thread; sum = 0; pthread_create(&child_thread, NULL, (create_func)child_function, NULL); puts("I think I'll waste a little time. Do you mind?"); for (i = 0; i < WASTE_TIME; i++) { sum += i; } pthread_cancel(child_thread); return EXIT_SUCCESS; }