#include #include #define TEST_JOIN int done; void *thr_routine(void *a) { char *p = malloc(2048); int i; for (i = 0; i < 2048; ++i) p[i] = i % 256; free(p); #ifndef TEST_JOIN pthread_detach(pthread_self()); #endif return (0); } void *create_thr(void *a) { pthread_t threads[100]; int i, ret; while (!done) { for (i = 0; i < 100; ++i) { if ((ret = pthread_create(&threads[i], 0, thr_routine, NULL)) != 0) errc(1, ret, "pthread_create"); } #ifndef TEST_JOIN sleep(2); #else for (i = 0; i < 100; ++i) { if ((ret = pthread_join(threads[i], NULL)) != 0) errc(1, ret, "pthread_join"); } #endif } return (0); } void handler(int sig) { done = 1; } int main() { pthread_t td1, td2; if (pthread_create(&td1, NULL, create_thr, NULL)) err(1, "can not create thread 1"); if (pthread_create(&td2, NULL, create_thr, NULL)) err(1, "can not create thread 2"); signal(SIGALRM, handler); alarm(60); #if 0 sleep(60); done = 1; #endif pthread_exit(0); return (0); }