// Программа из конспекта "Системное программное обеспечение" // Многопоточное программирование в Linux // стр. 119 // Название: thread2.c // Описание: создание и уничтожение потоков #include #include #include #include void *thread(void *num) { int i=*((int *) num); sleep(rand()%(i+1)); printf ("Thread #%d working hard!\n",i); pthread_exit(NULL); } int main (void) { pthread_t tid[10]; int i; for(i=0;i<10; i++) { printf("Starting %dth thread\n",i); pthread_create(&tid[i], NULL, thread, &i); } printf("All threads started!\n"); if (pthread_join(tid[0], NULL)) { for(i=1;i<10; i++) { pthread_cancel(tid[i]); } } printf ("All threads stopped!\n"); return (0); }