C語(yǔ)言中pthread_create函數(shù)實(shí)現(xiàn)向線程函數(shù)傳遞參數(shù)
一、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, ¶m1);
線程函數(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, ¶m1); 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)文章
C語(yǔ)言中怎么在main函數(shù)開(kāi)始前執(zhí)行函數(shù)
C語(yǔ)言中怎么在main函數(shù)開(kāi)始前執(zhí)行函數(shù)呢?下面小編就大家詳細(xì)的介紹一下。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10C語(yǔ)言中組成不重復(fù)的三位數(shù)問(wèn)題
這篇文章主要介紹了C語(yǔ)言中組成不重復(fù)的三位數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11C++數(shù)據(jù)結(jié)構(gòu)之實(shí)現(xiàn)循環(huán)順序隊(duì)列
這篇文章主要介紹了 C++數(shù)據(jù)結(jié)構(gòu)之實(shí)現(xiàn)循環(huán)順序隊(duì)列的相關(guān)資料,需要的朋友可以參考下2017-01-01c語(yǔ)言枚舉類型enum的用法及應(yīng)用實(shí)例
enum是C語(yǔ)言中的一個(gè)關(guān)鍵字,enum叫枚舉數(shù)據(jù)類型,枚舉數(shù)據(jù)類型描述的是一組整型值的集合,這篇文章主要給大家介紹了關(guān)于c語(yǔ)言枚舉類型enum用法及應(yīng)用的相關(guān)資料,需要的朋友可以參考下2021-07-07C++標(biāo)準(zhǔn)庫(kù)學(xué)習(xí)之weak_ptr智能指針用法詳解
這篇文章主要為大家詳細(xì)介紹了C++標(biāo)準(zhǔn)庫(kù)中weak_ptr智能指針用法的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12C++實(shí)現(xiàn)快捷店會(huì)員管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)快捷店會(huì)員管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03C++實(shí)現(xiàn)基于EASYX庫(kù)掃描線算法
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)基于EASYX庫(kù)掃描線算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02C++標(biāo)準(zhǔn)庫(kù)實(shí)現(xiàn)WAV文件讀寫(xiě)的操作
本文將使用標(biāo)準(zhǔn)C++庫(kù)實(shí)現(xiàn)對(duì)數(shù)據(jù)為PCM格式的WAV文件的讀寫(xiě)操作,只使用標(biāo)準(zhǔn)C++庫(kù)函數(shù),不依賴于其他的庫(kù),對(duì)C++標(biāo)準(zhǔn)庫(kù)實(shí)現(xiàn)WAV文件讀寫(xiě)相關(guān)知識(shí)感興趣的朋友一起看看吧2022-01-01C++實(shí)現(xiàn)LeetCode(121.買(mǎi)賣(mài)股票的最佳時(shí)間)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(121.買(mǎi)賣(mài)股票的最佳時(shí)間),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07