多线程代码
代码为:
1 #include <stdio.h>
2 #include <pthread.h>
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
");
10 pthread_join(t1,NULL);
11 pthread_join(t2,NULL);
12 printf("t1, t2 finished
");
13 return 0;
14 }
15 void *print_msg(void *m)
16 {
17 char *cp = (char *) m;
18 int i;
19 for(i=0;i<NUM;i++) {
20 printf("%s",m);
21 fflush(stdout);
22 sleep(1);
23 }
24 return NULL;
25 }
编译和运行命令截图: