欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語(yǔ)言中pthread_create函數(shù)實(shí)現(xiàn)向線程函數(shù)傳遞參數(shù)

 更新時(shí)間:2023年05月30日 16:11:04   作者:焱齒  
本文主要介紹了C語(yǔ)言中pthread_create函數(shù)實(shí)現(xiàn)向線程函數(shù)傳遞參數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、pthread_create函數(shù):

1、簡(jiǎn)介:pthread_create是UNIX環(huán)境創(chuàng)建線程的函數(shù)

2、頭文件:#include <pthread.h>

3、函數(shù)聲明:

int pthread_create(pthread_t* restrict tidp,const pthread_attr_t* restrict_attr,void* (*start_rtn)(void*),void *restrict arg);

4、輸入?yún)?shù):(以下做簡(jiǎn)介,具體參見(jiàn)實(shí)例一目了然)

(1)tidp:事先創(chuàng)建好的pthread_t類型的參數(shù)。成功時(shí)tidp指向的內(nèi)存單元被設(shè)置為新創(chuàng)建線程的線程ID。

(2)attr:用于定制各種不同的線程屬性。APUE的12.3節(jié)討論了線程屬性。通常直接設(shè)為NULL。

(3)start_rtn:新創(chuàng)建線程從此函數(shù)開(kāi)始運(yùn)行。無(wú)參數(shù)是arg設(shè)為NULL即可。

(4)arg:start_rtn函數(shù)的參數(shù)。無(wú)參數(shù)時(shí)設(shè)為NULL即可。有參數(shù)時(shí)輸入?yún)?shù)的地址。當(dāng)多于一個(gè)參數(shù)時(shí)應(yīng)當(dāng)使用結(jié)構(gòu)體傳入。(以下舉例)

5、返回值:成功返回0,否則返回錯(cuò)誤碼。

6、說(shuō)明。

傳遞參數(shù)的時(shí)候傳地址: pthread_create(&ntid, NULL, thr_fn, &param1);

線程函數(shù)的第一句通常是獲取傳入?yún)?shù):Param tmp = *(Param *)arg;

舉例如下:

二、不向線程函數(shù)傳遞參數(shù):

#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;
    //第四個(gè)參數(shù)為NULL,說(shuō)明沒(méi)有向線程函數(shù)傳參數(shù)。
	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);
}

三、向線程函數(shù)傳遞一個(gè)參數(shù):

#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;
    //向線程函數(shù)傳入一個(gè)參數(shù)。
	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);
}

四、向線程函數(shù)傳遞兩個(gè)或以上的參數(shù):

#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;
    //通過(guò)結(jié)構(gòu)體向線程函數(shù)傳入多個(gè)參數(shù)
	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);
}

執(zhí)行效果如下:

到此這篇關(guān)于C語(yǔ)言中pthread_create函數(shù)實(shí)現(xiàn)向線程函數(shù)傳遞參數(shù)的文章就介紹到這了,更多相關(guān)C語(yǔ)言 pthread_create函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論