一个不能编译通过的多线程示例程序

来源:互联网 发布:mac windows 共享软件 编辑:程序博客网 时间:2024/05/16 12:47
/* ------------------------------------------------------------------------------------------------- *                                         test3.c *  * Author   : 多线程*** * Date     : 2015-10-16 * Describe : 这是个十分简单的POSIX多线程程序。主线程创建两个子线程,并分别将1和2作为参数传递给子线 *            程,之后主线程将第一个子线程分离,将第二个子线程结合,之后主线程显示Hello world!。两个 *            子线程则执行相同的线程函数,先将参数pvoid转换成int型,然后再显示Hello world!,其中加入 *            了创建线程时传递的线程编号。 *                读者可以尝试将pthread_join语句去掉,或者换成pthread_exit(NULL),看看效果。 * Question : 该程序在Linux下,编译命令: # gcc -Wall test3.c -o test3  编译无法通过!原因待查! * ---------------------------------------------------------------------------------------------- */#include <stdio.h>#include <pthread.h>void * threadfunc(void *pvoid){int  id = (int) pvoid;printf("Child thread%d says: Hello world!\n", id);return NULL;}int main(void){pthread_t  tid1, tid2;pthread_create(&tid1, NULL, &threadfunc, 1);pthread_create(&tid2, NULL, &threadfunc, 2);pthread_detach(tid1);pthread_join(tid2, NULL);printf("Main thread says: Hello world!\n");return 0;}

一个有问题的示例。。。看来笔者可能没有经过上机验证
0 0
原创粉丝点击