b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

pthread_create 第一个参数_pthread_create 第一个参数_pthread_create传递多个

电脑杂谈  发布时间:2020-03-18 18:01:16  来源:网络整理

pthread_create 第一个参数_pthread_create传递多个参数_pthread_create 第一个参数

今天开始学习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

C99 中新增加了 restrict 修饰的指针: 由 restrict 修饰的指针是最初唯一对指针所指向的对象进行存取的方式,仅当第二个指针基于第一个时,才能对对象进行存取。对对象的存取都限定于基于由 restrict 修饰的指针表达式中。 由 restrict 修饰的指针主要用于变量赋值,或指向由 malloc() 分配的存储空间。restrict 数据类别不改变程序的词义。 编译器能通过做出 restrict 修饰的指针是存取对象的唯一途径的假定,更好地优化这些类别的示例。

第一个参数为指向线程标识符的指针。

pthread_create 第一个参数_pthread_create传递多个参数_pthread_create 第一个参数

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

第三个参数是泛型运行函数的起始地址。

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

下面这个程序中,我们的变量thr_fn不需要参数pthread_create 第一个参数,所以最后一个参数设为空指针。第二个参数我们也设为空指针,这样将生成默认属性的线程。当创建泛型成功时,函数返回0,若不为0则表明建立线程失败,常见的出错返回代码为EAGAIN和EINVAL。前者表示平台限制创建新的线程,例如线程数量过多了;后者表示第二个参数代表的句柄属性值非法。创建泛型成功后,新建立的线程则运行参数三和参数四确定的变量,原来的线程则再次运行下一行代码。

#include<stdio.h>

#include<pthread.h>

#include<string.h>

#include<sys/types.h>

#include<unistd.h>

pthread_create传递多个参数_pthread_create 第一个参数_pthread_create 第一个参数

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);

}

pthread_create 第一个参数_pthread_create 第一个参数_pthread_create传递多个参数

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));

pthread_create 第一个参数_pthread_create传递多个参数_pthread_create 第一个参数

return 1;

}

printids("main thread:");

sleep(1);

return 0;

}

把APUE2上的一个程序更改一下pthread_create 第一个参数,然后编译。

结果报错:

pthread.c:(.text+0x85):对‘pthread_create’未定义的引用

由于pthread库不是Linux平台默认的库,连接时必须使用库libpthread.a,所以在使用pthread_create创建泛型时,在编译中要加-lpthread参数:

gcc -o pthread -lpthread pthread.c


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-144607-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      • 赵江营
        赵江营

        卖假货的商家立案稽查了没有

      热点图片
      拼命载入中...