今天写线程,想看看 pthread_t 的实际类型是什么,vi /usr/include/pthread.h 头文件,最后找到 pthread_t 定义在#include <bits/pthreadtypes.h> 该头文件中,一步一步的找可以找出来,不过麻烦;不如用 gcc -E cfile.c -o cfile.e 预编译,然后grep pthread_t 就可以看到定义为:typedef unsigned long int pthread_t; 比较好用。
当然需要对gcc的编译几个阶段熟悉,gcc -${E,S,c} .. 和记住这个单词,逃生(escape),就可以记住。
[root@localhost pthread]# cat pthreadtest.c
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int * thread(void * thread)
{
pthread_t newthid;
newthid = pthread_self();
printf("new thread id is : %u \n",newthid);
}
int main(int argc,char ** argv,char **envp)
{
pthread_t thid;
if(pthread_create(&thid,NULL,(void*)thread,NULL)!=0)
{
printf("Create failed.\n");
exit(1);
}
sleep(1);
exit(0);
}
编译记得席上 gcc cfile.c -lpthread -o pthreadtest.bin
因为不在默认搜索路径上
[root@localhost pthread]# find / -name libpthread*
/usr/lib/libpthread_nonshared.a
/usr/lib/libpthread.so
/lib/libpthread.so.0
/lib/libpthread-2.12.so
/lib/i686/nosegneg/libpthread.so.0
/lib/i686/nosegneg/libpthread-2.12.so
----查看这个文件
[root@localhost pthread]# t=/usr/lib/libpthread.so && file $t && echo -e "\n\n"&& cat $t
/usr/lib/libpthread.so: ASCII C program text
/* GNU ld script
Use the shared library, but some functions are only in
the static library, so try that secondarily. */
OUTPUT_FORMAT(elf32-i386)
GROUP ( /lib/libpthread.so.0 /usr/lib/libpthread_nonshared.a )