1
2
3
4
5
6
7
8
9
10
11
12
for (int i = 0; i < THREAD_NUM; ++i) {
if (pthread_create(&tid[i], NULL, counter, (void *)&i) != 0) {
perror("pthread_create error");
exit(1);
}
}
for (int i = 0; i < THREAD_NUM; ++i) {
if (pthread_join(tid[i], NULL) != 0) {
perror("pthread_join error");
exit(1);
}
}

参考

  1. http://stackoverflow.com/questions/11624545/how-to-make-main-thread-wait-for-all-child-threads-finish

留言

2015-08-15