您的位置:首页 > 运维架构 > Linux

pthread_create

2016-06-25 14:37 417 查看

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  多线程 linux api