C語言中pthread_create函數(shù)實現(xiàn)向線程函數(shù)傳遞參數(shù)
一、pthread_create函數(shù):
1、簡介: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、輸入參數(shù):(以下做簡介,具體參見實例一目了然)
(1)tidp:事先創(chuàng)建好的pthread_t類型的參數(shù)。成功時tidp指向的內存單元被設置為新創(chuàng)建線程的線程ID。
(2)attr:用于定制各種不同的線程屬性。APUE的12.3節(jié)討論了線程屬性。通常直接設為NULL。
(3)start_rtn:新創(chuàng)建線程從此函數(shù)開始運行。無參數(shù)是arg設為NULL即可。
(4)arg:start_rtn函數(shù)的參數(shù)。無參數(shù)時設為NULL即可。有參數(shù)時輸入參數(shù)的地址。當多于一個參數(shù)時應當使用結構體傳入。(以下舉例)
5、返回值:成功返回0,否則返回錯誤碼。
6、說明。
傳遞參數(shù)的時候傳地址: pthread_create(&ntid, NULL, thr_fn, ¶m1);
線程函數(shù)的第一句通常是獲取傳入參數(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;
//第四個參數(shù)為NULL,說明沒有向線程函數(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ù)傳遞一個參數(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ù)傳入一個參數(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ù)傳遞兩個或以上的參數(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;
//通過結構體向線程函數(shù)傳入多個參數(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í)行效果如下:

到此這篇關于C語言中pthread_create函數(shù)實現(xiàn)向線程函數(shù)傳遞參數(shù)的文章就介紹到這了,更多相關C語言 pthread_create函數(shù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++用一棵紅黑樹同時封裝出set與map的實現(xiàn)代碼
set中存儲的一般為鍵K即可,而map存儲的一般都是鍵值對KV,也就是說他們結構是不同的,那么我們如何才能用一顆紅黑樹同時封裝出set與map兩種容器呢,那么接下來我們具體地來研究下STL庫中是怎樣實現(xiàn)的,并且進行模擬實現(xiàn),需要的朋友可以參考下2024-03-03

