您的位置:首页 > 其它

pthread_create()函数

2017-07-29 21:52 369 查看
(1) 函数原型:
int pthread_create (pthread_t *thread_tid , const pthread_attr_t *attr , void * ( * start_routine )( void * ), void *arg );
(2) 头文件:
#include <pthread.h>
(3) 函数功能:
创建一个新的线程
(4)  参数说明:
thread_tid:如果新线程创建成功,参数[b]thread_tid
返回新生成的线程的id,一个进程中的每个线程都有一个线程ID,其类型为[b]thread_tid;
[/b][/b]

attr:指向线程属性的指针,每个线程都有很多属性,包括优先级,起始栈大小,是否是守护线程等,通常将[b]attr参数的值设置为NULL,这是使用系统默认的属性。[/b]
[b]start_routine
:处理线程函数的地址,该函数必须是一个静态的函数,该函数参数是一个通用指针arg;

[/b]
arg:
[b]start_routine ()中的参数,如果想传递多个参数的话,需要将多个参数打包成结构体,然后将arg指向该结构体。
[/b]
(5) 返回值:
成功:0
失败:非零
(6) [b]编译和链接时引用-lpthread[/b]

(7) 用法:
#include <pthread.h>
pthread_t tid;
int arg;
void *function(void *arg);
if( pthread_create( &tid, NULL, function, (void*)&arg) )
{
//处理异常
exit(1);

}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐