代码;
#include
#include
#include
#include
#define NUM_THREADS 6
void *thread_function(void *arg);
int main()
{
int res;
pthread_t a_thread[NUM_THREADS];
void *thread_result;
int lots_of_threads;
for (lots_of_threads = 0; lots_of_threads < NUM_THREADS; lots_of_threads++) {
res = pthread_create(&(a_thread[lots_of_threads]), NULL,
thread_function, (void *) &lots_of_threads);
if (res != 0) {
perror("线程创建失败");
exit(EXIT_FAILURE);
}
sleep(1);
}
printf("等待线程结束...\n");
for (lots_of_threads = NUM_THREADS - 1; lots_of_threads >= 0; lots_of_threads--) {
res = pthread_join(a_thread[lots_of_threads], &thread_result);
if (res == 0) {
printf("结束一个线程\n");
} else {
perror("线程结束失败");
}
}
printf("线程全部结束\n");
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
int my_number = *(int *) arg;
int rand_num;
printf("线程函数已运行,参数为:%d\n", my_number);
rand_num = 1 + (int) (9.0 * rand() / (RAND_MAX + 1.0));
sleep(rand_num);
printf("第%d个线程结束\n", my_number);
pthread_exit(NULL);
}
运行
文章评论(0条评论)
登录后参与讨论