pthread_create

pthread_create

原型:
int pthread_create(pthread_t tid, const pthread_attr_t *attr, (void)(start_routine)(void), void *arg);


功能:创建线程(实际上就是确定调用该线程函数的入口点),在线程创建以后,就开始运行相关的线程函数。


参数:
tid:指向线程标识符的指针
attr:用来设置线程属性
start_routine:线程运行函数的起始地址
arg:传递给start_routine的参数


返回值:
若线程创建成功,则返回0。若线程创建失败,则返回出错编号,并且*thread中的内容是未定义的。错误编号:
EAGAIN
描述: 超出了系统限制,如创建的线程太多。
EINVAL
描述: tattr 的值无效。


说明:
如果未指定属性对象,则该对象为NULL,系统会创建具有以下属性的缺省线程: 进程范围、 非分离、 缺省栈和缺省栈大小、 零优先级。
还可以用pthread_attr_init() 创建缺省属性对象,然后使用该属性对象来创建缺省线程。
start_routine 是新线程最先执行的函数。
当start_routine 返回时,该线程将退出,其退出状态设置为由start_routine 返回的值。


示例:

#include <pthread.h>
pthread_attr_t() tattr;
pthread_t tid;
extern void *start_routine(void *arg);
void *arg;
int ret;
/* default behavior*/
ret = pthread_create(&tid, NULL, start_routine, arg);
/* initialized with default attributes */
ret = pthread_attr_init(&tattr);
/* default behavior specified*/
ret = pthread_create(&tid, &tattr, start_routine, arg);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值