C语言中pthread_create函数实现向线程函数传递参数

 更新时间:2023年05月30日 16:11:04   作者:焱齿  
本文主要介绍了C语言中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, &param1);

线程函数的第一句通常是获取传入参数: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, &param1);
	if (err != 0)
		err_exit(err, "can't create thread");
	printids("main thread:");
	sleep(1);
	exit(0);
}

执行效果如下:

到此这篇关于C语言中pthread_create函数实现向线程函数传递参数的文章就介绍到这了,更多相关C语言 pthread_create函数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C++用一棵红黑树同时封装出set与map的实现代码

    C++用一棵红黑树同时封装出set与map的实现代码

    set中存储的一般为键K即可,而map存储的一般都是键值对KV,也就是说他们结构是不同的,那么我们如何才能用一颗红黑树同时封装出set与map两种容器呢,那么接下来我们具体地来研究下STL库中是怎样实现的,并且进行模拟实现,需要的朋友可以参考下
    2024-03-03
  • C++约瑟夫环问题详解

    C++约瑟夫环问题详解

    大家好,本篇文章主要讲的是C++约瑟夫环问题详解 ,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2022-01-01
  • Qt实现TCP客户端和服务器通讯程序

    Qt实现TCP客户端和服务器通讯程序

    这篇文章主要为大家详细介绍了Qt实现TCP客户端和服务器通讯程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • C语言预处理预编译命令及宏定义详解

    C语言预处理预编译命令及宏定义详解

    这篇文章主要为大家介绍了C语言预处理预编译命令及宏定义的详解,其中包含运行环境命名约定条件及#under等基础详解,有需要的朋友可以借鉴参考下
    2021-10-10
  • Qt实现解压带有密码的加密文件

    Qt实现解压带有密码的加密文件

    Quazip是Qt平台下面的一个压缩解压缩库。本文将利用Quazip实现解压带有密码的加密文件,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-02-02
  • Qt实现打地鼠游戏的方法详解

    Qt实现打地鼠游戏的方法详解

    这篇文章主要和大家详细介绍了如何利用Qt实现一个简单的打地鼠游戏,文中的示例代码讲解详细,具有一定的借鉴价值,需要的可以参考一下
    2022-10-10
  • C语言Static 关键字解析

    C语言Static 关键字解析

    这篇文章主要介绍了C语言Static 关键字解析,C语言中staic关键字很简单,简单到你的任何一个项目中可以不写一个staic关键字也是没有问题的。写这篇章主要是一下自己的staic的理解和应用,当然在章开头依旧要照本宣科简述一下static关键字,需要的朋友可以参考一下
    2022-02-02
  • C++实现获取时间戳和计算运行时长

    C++实现获取时间戳和计算运行时长

    这篇文章主要为大家详细介绍了如何使用C++实现获取时间戳和计算运行时长功能,文中的示例代码讲解详细,有需要的小伙伴可以参考一下
    2024-12-12
  • C语言实现打印数字金字塔

    C语言实现打印数字金字塔

    这篇文章主要介绍了C语言实现打印数字金字塔方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • C语言控制台应用程序GDI绘制正弦曲线

    C语言控制台应用程序GDI绘制正弦曲线

    这篇文章主要为大家详细介绍了C语言控制台应用程序GDI绘制正弦曲线,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06

最新评论