20199317 多线程代码

多线程代码

代码为:

 1 #include 
 2 #include 
 3 #define NUM 5
 4 void *print_msg(void *);
 5 int main()
 6 {
 7     pthread_t t1,t2;
 8     pthread_create(&t1,NULL,print_msg,(void *)"hello");
 9     pthread_create(&t2,NULL,print_msg,(void *)"world\n");
10     pthread_join(t1,NULL);
11     pthread_join(t2,NULL);
12     printf("t1, t2 finished\n");
13     return 0;
14 }
15 void *print_msg(void *m)
16 {
17     char *cp = (char *) m;
18     int i;
19     for(i=0;i) {
20         printf("%s",m);
21         fflush(stdout);
22         sleep(1);
23     }
24     return NULL;
25 }

编译和运行命令截图:

20199317 多线程代码_第1张图片

你可能感兴趣的:(20199317 多线程代码)