- 论坛徽章:
- 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);
- }
复制代码 编译:
gcc a.c -lpthread生成a.out
执行a.out输出
tid=3086244752
tid=3075750800
tid=3065260944
tid=3054771088
tid=3044281232
而通过执行pstree 命令
$ pstree -p 17384
其中17384是进程号
a.out(17384)-+-{a.out}(17385)
|-{a.out}(17386)
|-{a.out}(17387)
|-{a.out}(1738
`-{a.out}(17389)
为什么打印的不对呢?
用printf("%u",tid)也相同
用printf("%d",tid)竟然是负数,当然也不对。
该如何输出呢? |
|