linux编程之pthread_create函数

linux编程之pthread_create函数
2011-04-12 14:24

UNIX环境创建线程函数,
具体格式:  
#include<pthread.h>   
int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);   
返回值:
若成功则返回0,否则返回出错编号  
返回成功时,由tidp指向的内存单元被设置为新创建线程的线程ID。
attr参数用于制定各种不同的线程属性。新创建的线程从start_rtn函数的地址开始运行,该函数只有一个无指针参数arg,如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg的参数传入。  

linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread。  
#include <pthread.h>int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void), void *restrict arg);
Returns: 0 if OK, error number on failure
 由 restrict 修饰的指针是最初唯一对指针所指向的对象进行存取的方法,仅当第二个指针基于第一个时,才能对对象进行存取。
对对象的存取都限定于基于由 restrict 修饰的指针表达式中。
 由 restrict 修饰的指针主要用于函数形参,或指向由 malloc() 分配的内存空间。
restrict 数据类型不改变程序的语义。
编译器能通过作出 restrict 修饰的指针是存取对象的唯一方法的假设,更好地优化某些类型的例程。  
第一个参数为指向线程标识符的指针。  
第二个参数用来设置线程属性。  
第三个参数是线程运行函数的起始地址。  
最后一个参数是运行函数的参数。  
另外,在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库

线程创建函数:
int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void * (*func)(void *), void *arg);
参数func 表示代一个参数void *,返回值也为void *;
对于void *arg,参数传入,在gcc 3.2.2条件下,以下面两种方式传入都可编译通过。
int ssock;
int TCPechod(int fd);
1.pthread_create(&th, &ta, (void *(*)(void *))TCPechod, (void *)ssock);
2.pthread_create(&th, &ta, (void *(*)(void *))&TCPechod, (void *)&ssock);

============================================

Linux系统下的多线程遵循POSIX线程接口,称为pthread。

#include <pthread.h>

int pthread_create(pthread_t *restrict tidp,

const pthread_attr_t *restrict attr,

void *(*start_rtn)(void),

void *restrict arg);

Returns: 0 if OK, error number on failure

restrict它是c99中新增加的类型定义,这里先不管,为了好理解,直接把它去掉就行。第一个参数为指向线程标识符的指针。

第二个参数用来设置线程属性。

第三个参数是线程运行函数的起始地址。

最后一个参数是运行函数的参数。

下面这个程序中,我们的函数

thr_fn

不需要参数,所以最后一个参数设为空指针。第二个参数我们也设为空指针,这样将生成默认属性的线程。当创建线程成功时,函数返回0,若不为0则说明创建线程失败,常见的错误返回代码为EAGAIN和EINVAL。前者表示系统限制创建新的线程,例如线程数目过多了;后者表示第二个参数代表的线程属性值非法。创建线程成功后,新创建的线程则运行参数三和参数四确定的函数,原来的线程则继续运行下一行代码。
#include<stdio.h>

#include<pthread.h>

#include<string.h>

#include<sys/types.h>

#include<unistd.h>

pthread_t ntid;

void printids(const char *s)
{
pid_t pid;

pthread_t tid;

pid = getpid();

tid = pthread_self();

printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,(unsigned int)tid,(unsigned

int)tid);
}

void *thr_fn(void *arg)
{
printids("new thread:");
return ((void *)0);
}

int main()
{
int err;
err = pthread_create(&ntid,NULL,thr_fn,NULL);
if(err != 0)
{
printf("can't create thread: %s\n",strerror(err));
return 1;
}
printids("main thread:");
sleep(1);
return 0;
}

把APUE2上的一个程序修改一下,然后编译。
结果报错:
pthread.c:(.text+0x85):对‘pthread_create’未定义的引用

由于pthread库不是Linux系统默认的库,连接时需要使用库libpthread.a,所以在使用pthread_create创建线程时,在编译中要加-lpthread参数:

gcc -o pthread -lpthread pthread.c


Linux系统下的多线程遵循POSIX线程接口,称为pthread

view plaincopy to clipboardprint?
#include <pthread.h>  
/*  
功能:创建线程  
参数:        thread_id:     指向线程标识符的指针  
attr:          设置线程属性,NULL为默认属性  
start_routine: 指向线程运行函数的指针                     
arg:           线程运行函数的参数  
返回值:      成功:0  
失败:错误代码  
*/  
int pthread_create(pthread_t *restrict thread_id,  
const pthread_attr_t *restrict attr,  
void *(*start_routine)(void *),   
void *restrict arg); 
#include <pthread.h>
/*
功能:创建线程
参数:        thread_id:     指向线程标识符的指针
attr:          设置线程属性,NULL为默认属性
start_routine: 指向线程运行函数的指针                  
arg:           线程运行函数的参数
返回值:      成功:0
失败:错误代码
*/
int pthread_create(pthread_t *restrict thread_id,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void *),
void *restrict arg);


与fork()调用创建一个进程的方法不同,pthread_create()创建的线程并不具备与主线程(即调用pthread_create()的线程)同样的执行序列,而是使其运行start_routine(arg))函数。

thread_id返回创建的线程ID

attr参数是一个结构指针,用于设置的线程属性(见下)。结构中的元素分别对应着新线程的运行属性,主要包括以下几项:__detachstate,表示新线程是否与进程中其他线程脱离同步,如果置位则新线程不能用pthread_join()来同步,且在退出时自行释放所占用的资源。缺省为PTHREAD_CREATE_JOINABLE状态。这个属性也可以在线程创建并运行以后用pthread_detach()来设置,而一旦设置为PTHREAD_CREATE_DETACH状态(不论是创建时设置还是运行时设置)则不能再恢复到 PTHREAD_CREATE_JOINABLE状态。

__schedpolicy,表示新线程的调度策略,主要包括SCHED_OTHER(正常、非实时)、SCHED_RR(实时、轮转法)和 SCHED_FIFO(实时、先入先出)三种,缺省为SCHED_OTHER,后两种调度策略仅对超级用户有效。运行时可以用过 pthread_setschedparam()来改变。

__schedparam,一个struct sched_param结构,目前仅有一个sched_priority整型变量表示线程的运行优先级。这个参数仅当调度策略为实时(即SCHED_RR 或SCHED_FIFO)时才有效,并可以在运行时通过pthread_setschedparam()函数来改变,缺省为0。

__inheritsched,有两种值可供选择:PTHREAD_EXPLICIT_SCHED和PTHREAD_INHERIT_SCHED,前者表示新线程使用显式指定调度策略和调度参数(即attr中的值),而后者表示继承调用者线程的值。缺省为PTHREAD_EXPLICIT_SCHED。

__scope,表示线程间竞争CPU的范围,也就是说线程优先级的有效范围。POSIX的标准中定义了两个值:PTHREAD_SCOPE_SYSTEM和PTHREAD_SCOPE_PROCESS,前者表示与系统中所有线程一起竞争CPU时间,后者表示仅与同进程中的线程竞争CPU。目前LinuxThreads仅实现了PTHREAD_SCOPE_SYSTEM一值。

pthread_attr_t结构中还有一些值,但不使用pthread_create()来设置。

为了设置这些属性,POSIX定义了一系列属性设置函数,包括pthread_attr_init()、pthread_attr_destroy()和与各个属性相关的pthread_attr_get---/pthread_attr_set---函数。

pthread_create()的返回值表示线程创建是否成功。常见的错误返回代码为EAGAIN和EINVAL。前者表示系统限制创建新的线程,例如线程数目过多了;后者表示第二个参数代表的线程属性值非法。

尽管arg是void *类型的变量,但它同样可以作为任意类型的参数传给start_routine()函数;同时,start_routine()可以返回一个void *类型的返回值,而这个返回值也可以是其他类型,并由pthread_join()获取。


原文地址:https://www.cnblogs.com/yuzaipiaofei/p/4124629.html