pthread_create函数
函数简介
pthread_create是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,否则返回出错编号
参数
第一个参数为指向线程标识符的指针。
第二个参数用来设置线程属性。
第三个参数是线程运行函数的地址。
最后一个参数是运行函数的参数。
注意
在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库。
pthread_join函数
函数简介
函数pthread_join用来等待一个线程的结束。
函数原型为:
extern int pthread_join __P (pthread_t __th, void **__thread_return);
参数:
第一个参数为被等待的线程标识符
第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。
注意
这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。如果执行成功,将返回0,如果失败则返回一个错误号。
例子:

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h> /* 声明结构体 */
struct member
{
int num;
char *name;
}; /* 定义线程pthread */
static void * pthread(void *arg)
{
struct member *temp; /* 线程pthread开始运行 */
printf("pthread start!\n"); /* 令主线程继续执行 */
sleep(2); /* 打印传入参数 */
temp = (struct member *)arg;
printf("member->num:%d\n",temp->num);
printf("member->name:%s\n",temp->name); return NULL;
} /* main函数 */
int main(int agrc,char* argv[])
{
pthread_t tidp;
struct member *b; /* 为结构体变量b赋值 */
b = (struct member *)malloc(sizeof(struct member));
b->num=1;
b->name="mlq"; /* 创建线程pthread */
if ((pthread_create(&tidp, NULL, pthread, (void*)b)) == -1)
{
printf("create error!\n");
return 1;
} /* 令线程pthread先运行 */
sleep(1); /* 线程pthread睡眠2s,此时main可以先执行 */
printf("mian continue!\n"); /* 等待线程pthread释放 */
if (pthread_join(tidp, NULL))
{
printf("thread is not exit...\n");
return -2;
} return 0;
}
编译与执行结果
编译与执行结果如下图所示,可以看到主线程main和线程pthread交替执行。也就是说是当我们创建了线程pthread之后,两个线程都在执行,证明创建成功。另外,可以看到创建线程pthread时候,传入的参数被正确打印。

Over。。。
本文转自:https://www.cnblogs.com/amanlikethis/p/5537175.html
pthread_create函数的更多相关文章
- 类成员函数作为pthread_create函数参数
from:http://www.cnblogs.com/shijingxiang/articles/5389294.html 近日需要将线程池封装成C++类,类名为Threadpool.在类的成员函数 ...
- linux下的多线程,pthread_create函数
pthread_create是UNIX环境创建线程函数 头文件 #include<pthread.h> 函数声明 int pthread_create(pthread_t*restrict ...
- 对线程等待函数pthread_join二级指针参数分析
分析之前先搞明白,这个二级指针其实在函数内部是承接了上个线程的返回值. 看man手册,发现返回值是个普通指针.人家用二级指针来承接,可能准备干大事.这个可以自己搜索一下.原因嘛,二级指针是保存了这个地 ...
- pthread_create如何传递两个参数以上的参数
涉及多参数传递给线程的,都需要使用结构体将参数封装后,将结构体指针传给线程 定义一个结构体 struct mypara { var para1;//参数1 var para2;//参数2 } 将这个结 ...
- 动态链接库中函数的地址确定---PLT和GOT [转]
前面写过动态链接库 延迟绑定的一篇博文,那篇文章我非常喜欢,但是当时刚搞清楚,自己写的比较凌乱,我最近学习了Ulrich Drepper的How to write share library,学习了几 ...
- linux创建线程之pthread_create
说明:本文转自多线程编程之pthread_create函数应用,在此基础上笔者做了些许改动. pthread_create函数 函数简介 pthread_create是UNIX环境创建线程函数 头文件 ...
- pthread_create()之前的属性设置
一.pthread_create()之前的属性设置1.线程属性设置我们用pthread_create函数创建一个线程,在这个线程中,我们使用默认参数,即将该函数的第二个参数设为NULL.的确,对大多数 ...
- pthread_create用法
linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread. #include <pthread.h> int pthread_create(pth ...
- 关于pthread里面一些函数的使用心得!
第一次使用pthread,遇到的问题还真不少,现在我一一记录一下: 1.关于编译时出现 对‘pthread_create’未定义的引用 之类的错误的解决:由于pthread库不是Linux系统默认的库 ...
随机推荐
- idea 启动热部署Devtolls
1.在子工程pom.xml中添加devtools jar包到需要启动的项目中 1 <dependency> 2 <groupId>org.springframework.boo ...
- Hadoop2.7.7阿里云安装部署
阿里云的网络环境不需要我们配置,如果是在自己电脑上的虚拟机,虚拟机的安装步骤可以百度.这里是单机版的安装(也有集群模式的介绍)使用Xshell连接阿里云主机,用命令将自己下载好的安装包上传到服务器 # ...
- 学习Java第一天
public 保证类名和文件名一致 关键字字母全小写,编辑器中有颜色标记 null空常量不能打印 变量就是内存中的存储空间 计算机中最小的存储单元时字节(byte) //1字节(B) = 8位(bit ...
- 使用注解的形式对token进行验证
@[TOC](使用注解的形式对token进行验证)# 前言现在很多系统都是都用上了springboot.springcloud,系统也偏向分布式部署.管理,最早的用户令牌方案:session.cook ...
- Covering Indexes in MySQL, PostgreSQL, and MongoDB
Covering Indexes in MySQL, PostgreSQL, and MongoDB - Orange Matter https://orangematter.solarwinds.c ...
- maven打包三种方式
https://blog.csdn.net/w820896059/article/details/80423143
- cookie中的domain和path
div.example { background-color: rgba(229, 236, 243, 1); color: rgba(0, 0, 0, 1); padding: 0.5em; mar ...
- (五)SpringBoot面试题
SpringBoot面试题 1.Spring Boot 的自动配置是如何实现的? 2.shiro和oauth还有cas他们之间的关系是什么?问下您公司权限是如何设计,还有就是这几个概念的区别. 2.1 ...
- JSP标签使用的代码记录——《%= %》(神奇的CSDN为啥标题不让打英文的尖括号)
关于JSP的一些标签,在用到的时候有些生疏,就去找了找资源重新温习了一下. 附上两个JSP<%= %>标签的博客,同时也记录当前项目里用到的方法. jsp页面中<%@ %>.& ...
- Java8 ,LocalDate,LocalDateTime处理日期和时间工具类,
Java8 ,LocalDate,LocalDateTime处理日期和时间工具类 1.获取今天的日期 2.在Java 8 中获取年.月.日信息 3.在Java 8 中处理特定日期 4.在Java 8 ...