pthread_create函数详解(向线程函数传递参数)
一、pthread_create函数:
1、简介:pthread_create是UNIX环境创建线程的函数
2、头文件:#include <pthread.h>
3、函数声明:
int pthread_create(pthread_t* restrict tidp,const pthread_attr_t* restrict_attr,void* (*start_rtn)(void*),void *restrict arg);
4、输入参数:(以下做简介,具体参见实例一目了然)
(1)tidp:事先创建好的pthread_t类型的参数。成功时tidp指向的内存单元被设置为新创建线程的线程ID。
(2)attr:用于定制各种不同的线程属性。APUE的12.3节讨论了线程属性。通常直接设为NULL。
(3)start_rtn:新创建线程从此函数开始运行。无参数是arg设为NULL即可。
(4)arg:start_rtn函数的参数。无参数时设为NULL即可。有参数时输入参数的地址。当多于一个参数时应当使用结构体传入。(以下举例)
5、返回值:成功返回0,否则返回错误码。
6、说明。
传递参数的时候传地址: pthread_create(&ntid, NULL, thr_fn, ¶m1);
线程函数的第一句通常是获取传入参数:Param tmp = *(Param *)arg;
举例如下:
二、不向线程函数传递参数:
#include "apue.h"
#include <pthread.h>
#include "apueerror.h"
#include <iostream>
#include <string>using namespace std;pthread_t ntid;void printids(const char *s){pid_t pid;pthread_t tid;pid = getpid();tid = pthread_self();printf("%s pid %lu tid %lu (0x%lx)\n", s, (unsigned long)pid,(unsigned long)tid, (unsigned long)tid);
}void *thr_fn(void *arg){cout << "----enter sub thread--------" << endl;printids("new thread: ");cout << "Change to C++ code!!" << endl;cout << "----exit from sub thread----" << endl;return((void *)0);
}int main(void){int err;//第四个参数为NULL,说明没有向线程函数传参数。err = pthread_create(&ntid, NULL, thr_fn, NULL);if (err != 0)err_exit(err, "can't create thread");printids("main thread:");sleep(1);exit(0);
}
三、向线程函数传递一个参数:
#include "apue.h"
#include <pthread.h>
#include "apueerror.h"
#include <iostream>
#include <string>
using namespace std;
pthread_t ntid;
void printids(const char *s){pid_t pid;pthread_t tid;pid = getpid();tid = pthread_self();printf("%s pid %lu tid %lu (0x%lx)\n", s, (unsigned long)pid,(unsigned long)tid, (unsigned long)tid);
}struct Param {int a;int b;int c;
};void *thr_fn( void *arg ) {cout << "----enter sub thread--------" << endl;int tmp = *(int *)arg;cout << "tmp=" << tmp << endl;printids("new thread: ");cout << "Change to C++ code!!" << endl;cout << "----exit from sub thread----" << endl;return((void *)0);
}int main(void){int err;int num = 123;//向线程函数传入一个参数。err = pthread_create(&ntid, NULL, thr_fn, &num);if (err != 0)err_exit(err, "can't create thread");printids("main thread:");sleep(1);exit(0);
}
四、向线程函数传递两个或以上的参数:
#include "apue.h"
#include <pthread.h>
#include "apueerror.h"
#include <iostream>
#include <string>
using namespace std;
pthread_t ntid;void printids(const char *s){pid_t pid;pthread_t tid;pid = getpid();tid = pthread_self();printf("%s pid %lu tid %lu (0x%lx)\n", s, (unsigned long)pid,(unsigned long)tid, (unsigned long)tid);
}struct Param {int a;int b;int c;
};void *thr_fn(void *arg) {cout << "----enter sub thread--------" << endl;Param tmp = *(Param *)arg;cout << "tmp.a=" << tmp.a << endl;cout << "tmp.b=" << tmp.b << endl;cout << "tmp.c=" << tmp.c << endl;printids("new thread: ");cout << "Change to C++ code!!" << endl;cout << "----exit from sub thread----" << endl;return((void *)0);
}int main(void){int err;int num = 123;Param param1;param1.a = 11;param1.b = 22;param1.c = 33;//通过结构体向线程函数传入多个参数err = pthread_create(&ntid, NULL, thr_fn, ¶m1);if (err != 0)err_exit(err, "can't create thread");printids("main thread:");sleep(1);exit(0);
}
执行效果如下:
执行程序下载传送门 这里
互联网面经(需要的同学可以参考学习):这里
pthread_create函数详解(向线程函数传递参数)相关推荐
- php output详解,PHP输出缓冲控制Output Control系列函数详解,output函数详解
PHP输出缓冲控制Output Control系列函数详解,output函数详解 概述 以前研究过PHP的输入输出缓冲,不过博客搬家以后,原来文章找不到了,今天看到一篇好文,顺便转载过来. 简介 说到 ...
- MaxCompute SQL函数详解 ODPS SQL函数详解---之日期相关函数
MaxCompute SQL函数详解 ODPS SQL函数详解 日期函数 to_date函数 返回类型:datetime 语法:to_date(类型 参数1,类型 参数2);to_date(strin ...
- MaxCompute SQL函数详解 ODPS SQL函数详解---之常用数学运算相关函数
MaxCompute SQL函数详解 ODPS SQL函数详解---之常用数学运算相关函数 MaxCompute/ODPS SQL常用数学运算相关函数 ABS函数-计算绝对值 sql:select A ...
- mysqlsum绝对值_MySQL_MySQL笔记之数学函数详解,绝对值函数ABS(x)和圆周率函数P - phpStudy...
MySQL笔记之数学函数详解 绝对值函数ABS(x)和圆周率函数PI() mysql> SELECT ABS(0.5), ABS(-0.5), PI(); +----------+------- ...
- python自定义函数详解_Python自定义函数
自定义函数 使用def定义函数,例如: def my_abs(x) : if x >= 0 : return x else : return -x 函数可以返回多个值,但是这是假象,实际上函数返 ...
- c语言函数详解1——自定义函数
这篇文章理论性知识比较多,不过对于初学者对于函数的理解会有更深层次的帮助 目录 c语言的函数--子程序 自定义函数 c语言的函数--子程序 *是大型程序中的某部分代码,由一个或者多个语句组成,她负责完 ...
- c++:内联函数详解和普通函数的区别
文章目录 前言 Ⅰ.常规函数 Ⅱ.内联函数 1.语法 2.注意 Ⅲ.选择地使用内联 思考? 前言 内联函数是c++为了提高程序的运行速度做的改进,它与普通函数区别在于: 编译器如何将它们组合到程序中. ...
- php 时间函数详解,PHP时间函数date()详解
html> /* php语言中默认设置的是标准的格林威治时间(即采用的是零时区),与我们本地的时间相差8个小时.所以要获取本地当前时间必须要更改PHP语言中的时区设置,只需在date()函数前加 ...
- python的groupby中函数详解_python groupby函数用法
import pandas as pd io = r'C:\Users\Administrator\Desktop\ceshi\ceshi.xlsx' data0 = pd.read_excel(io ...
- inline 内联函数详解 内联函数与宏定义的区别
一.在C&C++中 一.inline 关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 表达式形式的宏定义一例: #define ExpressionName ...
最新文章
- 你的产品没有一炮而红,后面该怎么办?
- 第三课.python编程基础(二)
- 文巾解题 695. 岛屿的最大面积
- python内置数据结构教程第四版答案_Python数据结构--内置数据结构
- php url地址 怎么写,php url地址重写
- 细数SkyEye异构仿真的5大特色
- 2016年象行中国(上海站)圆满结束,会议PPT分享
- 搭建Mybatis源码环境
- 修复ie浏览器无访问页面或dns问题
- DataX二次开发——(6)kafkareader、kafkawriter的开发
- Synch4j使用指南
- JavaWEB(AJAX实现分页)
- 通过前序序列和中序序列或中序序列和后序序列还原二叉树(Java)
- 关于java.util.concurrent.RejectedExecutionException: event executor terminated
- 痞子衡嵌入式:其实i.MXRT1050,1020,1015系列ROM也提供了FlexSPI driver API
- PS制作海报操作技巧若干,问题若干,查看图片大小
- 树莓派安装部署OpenVINO
- W ndows7怎么进入BlOS教程,Ghost windows7系统中bios设置u盘启动图文教程
- 移动云物联网预研及阿里云开发对比分析
- 西安市基础教育小课题研究结题报告