- 論壇徽章:
- 0
|
程序a.c如下- #include <pthread.h>
- #include <stdio.h>
- #include <unistd.h>
- void *myThread(void *arg)
- {
- sleep(1000);
- return 0;
- }
- main()
- {
- int ret;
- char tmpstr[20];
- pthread_t mythread=0;
- ret = pthread_create( &mythread, NULL, myThread, NULL );
- printf("tid=%lu\n",mythread);
- ret = pthread_create( &mythread, NULL, myThread, NULL );
- printf("tid=%lu\n",mythread);
- ret = pthread_create( &mythread, NULL, myThread, NULL );
- printf("tid=%lu\n",mythread);
- ret = pthread_create( &mythread, NULL, myThread, NULL );
- printf("tid=%lu\n",mythread);
- printf("tid=%lu\n",mythread);
- sleep(1000);
- }
復(fù)制代碼 編譯:
gcc a.c -lpthread生成a.out
執(zhí)行a.out輸出
tid=3086244752
tid=3075750800
tid=3065260944
tid=3054771088
tid=3044281232
而通過(guò)執(zhí)行pstree 命令
$ pstree -p 17384
其中17384是進(jìn)程號(hào)
a.out(17384)-+-{a.out}(17385)
|-{a.out}(17386)
|-{a.out}(17387)
|-{a.out}(1738
`-{a.out}(17389)
為什么打印的不對(duì)呢?
用printf("%u",tid)也相同
用printf("%d",tid)竟然是負(fù)數(shù),當(dāng)然也不對(duì)。
該如何輸出呢? |
|