- 論壇徽章:
- 0
|
重新寫了一個(gè)線程創(chuàng)建函數(shù),但是這次能正常運(yùn)行了。但是我發(fā)的第一個(gè)線程程序錯(cuò)在哪里?還請老師們解答
源程序
#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);
}
下面是鏈接運(yùn)行結(jié)果
[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
小弟跪謝了,還請指教啊 |
|