pthread_create函数详解(向线程函数传递参数)

一、pthread_create函数:

1、简介:pthread_create是UNIX环境创建线程的函数

2、头文件:#include

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 
#include "apueerror.h"
#include 
#include 

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 
#include "apueerror.h"
#include 
#include 
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 
#include "apueerror.h"
#include 
#include 
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函数详解(向线程函数传递参数)_第1张图片

执行程序下载传送门   这里  

你可能感兴趣的:(c++,c语言,gnu)