- 论坛徽章:
- 0
|
重新写了一个线程创建函数,但是这次能正常运行了。但是我发的第一个线程程序错在哪里?还请老师们解答
源程序
#include "stdio.h"
#include "pthread.h"
pthread_once_t once =PTHREAD_ONCE_INIT;
void run(void)
{
printf("Fuction run is running in thread %u\n",pthread_self());
}
int thread1(void)
{
pthread_t thid=pthread_self();
printf("Current thread1's ID is %u\n",thid);
pthread_once(&once,run);
printf("thread1 end\n");
return 0;
}
int thread2(void)
{
pthread_t thid=pthread_self();
printf("Current thread2's ID is %u\n",thid);
pthread_once(&once,run);
printf("thread2 end\n");
return 0;
}
int main()
{
pthread_t thid1,thid2;
pthread_create(&thid1,NULL,(void *(*)(void *))thread1,NULL);
pthread_create(&thid2,NULL,(void *(*)(void *))thread2,NULL);
sleep(3);
printf("main thread exit!,main thread ID is:%u\n",pthread_self());
exit(0);
}
下面是链接运行结果
[root@LinuxFzb tmp]# vi thid2.c
[root@LinuxFzb tmp]# gcc -o thid2 thid2.c -lpthread
[root@LinuxFzb tmp]# ./thid2
Current thread1's ID is 1082350784
Fuction run is running in thread 1082350784
thread1 end
Current thread2's ID is 1090739264
thread2 end
main thread exit!,main thread ID is:1073959552
小弟跪谢了,还请指教啊 |
|