函数声明
int pthread_create( pthread_t*restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);参数
attr参数用于指定各种不同的线程属性。新创建的线程从start_rtn函数的地址开始运行,该函数只有一个万能 指针参数arg, 如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg的参数传入。(百科),要注意,把这些参数放在一个结构中,一定也要在函数start_rtn()中这么做,也就是说这个参数在两个函数中一致。参考下面例子
返回值
若成功则返回0,否则返回出错编号 返回成功时,由tidp指向的内存单元被设置为新创建线程的线ID。linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread。
//sub_add.c实现加法和减法并行运算
#include
#include
struct arg{
};
void add(struct arg *ag)
{
}
void sub(struct arg*ag)
{
}
int main()
{
}
编译语句:gcc sub_add.c -o test -lpthread
运行语句:./test