Linux下 多线程编程

ret = pthread_create(&id,NULL,(void*)readThread,NULL);
if(ret!=0)
{
printf("creat pthread id1 error : %s(errno : %d)\n",strerror(errno),errno);
}

创建一个线程

头文件:

#include <pthread.h>

 

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);

返回值 0表示成功,-1表示失败

第一个参数:指向线程标识符的指针

第二个参数:设置线程属性

第三个参数:线程运行函数的地址(函数名)

第四个参数:运行函数的参数

 

gcc 编译需要手动链接库  -lpthread   (如 gcc xxx.c xxx -lpthread)

原因见:linux网络编程2.2.9

 

线程终止

void pthread_exit(void* retval);

exit(0)

 

posted @ 2017-09-08 21:18  robin1994  阅读(102)  评论(0)    收藏  举报