多線程的實(shí)現(xiàn)方式,只做記錄,自己看。
新建thread對(duì)象,傳入類的成員函數(shù)名稱,對(duì)象地址,以及成員函數(shù)需要的參數(shù)。
在類的成員函數(shù)中,要實(shí)現(xiàn)多線程
void Perception::test()
{.....
thread th( &Perception::showImage, this); //實(shí)例化一個(gè)線程對(duì)象th,使用函數(shù)t構(gòu)造,然后該線程就開始執(zhí)行了( &Perception::showImage)
.....
}
void Perception::showImage();
如果showImage()要傳參數(shù),就在this后面加上。
第二種 在類外在主函數(shù)中,
void main()
{A a;
thread th(&A::test,&a);
// 如果要傳參數(shù),就在&a后面加上。
}
第三種 沒有類void func()
{.....
}
void main()
{A a;
thread th(func);
// 如果要傳參數(shù),就在&a后面加上。
}
第四種 pthread,定時(shí)觸發(fā)使用定時(shí)器觸發(fā),傳入多個(gè)參數(shù)。
定時(shí)器實(shí)現(xiàn)是在回調(diào)函數(shù)中的:
double process_time = double(clock() - time_start) / CLOCKS_PER_SEC * 100;
// ROS_ERROR_STREAM(" process_time......"<< process_time);
pthread_testcancel();
struct timeval tempval;
tempval.tv_sec = 0;
tempval.tv_usec = 100000; // 10hz
select(0, NULL, NULL, NULL, &tempval);
其中,多個(gè)參數(shù)的定義在TIMECALL(結(jié)構(gòu)體),先對(duì)結(jié)構(gòu)體成員進(jìn)行實(shí)例化、賦值,然后在將timecall(取地址)傳入。
TIMECALL timecall;
timecall.log = &log_data;
timecall.outdata = &serial_output;
timecall.udp_output = &udp_output; // 20220801 yuphe 需要設(shè)置UDP
timecall.threadPerception = &per; // 20220801 yuphe 獲取數(shù)據(jù)
timecall.m_LogFile = &file_check; // 20221102 yuphe 日志內(nèi)存檢測(cè)
void main()
{TIMECALL timecall;
timecall.log = &log_data;
timecall.outdata = &serial_output;
timecall.udp_output = &udp_output; // 20220801 yuphe 需要設(shè)置UDP
timecall.threadPerception = &per; // 20220801 yuphe 獲取數(shù)據(jù)
timecall.m_LogFile = &file_check; // 20221102 yuphe 日志內(nèi)存檢測(cè)
// timecall.m_RosLog =
pthread_t id;
int i, ret;
ret = pthread_create(&id, NULL, thread_cb, (void *)&timecall);
}
typedef struct TIMECALL_
{Log *log;
result_serial::perception_result_serial *outdata;
UDPSocket *udp_output = nullptr; // yuphe 20220801
perception::Perception *threadPerception = nullptr; // yuphe 20220801
FileManage *m_LogFile= nullptr; //增加內(nèi)存檢測(cè) 20221102
FileManage *m_RosLog = nullptr; //增加ROS日志檢測(cè) 20221102
} TIMECALL;
void *thread_cb(void *p) // void *serialOut
{TIMECALL *arg = (TIMECALL *)p;
clock_t time_start = clock();
while (true)
{// sizeof(Perception_output_proto) 346
perception_output = arg->threadPerception->getOutdata();
// print out data
// for (int i = 0; i< 20; i++)
// {// std::cout<< "OBJECT:"<< i
// << " ID = "<< ((int)perception_output.proto_objects[i].NoID)
// << " Pos = ("<< (((double)perception_output.proto_objects[i].x) / 100)<< " , "<< (((double)perception_output.proto_objects[i].y) / 100)
// << ") Size = "<< (((double)perception_output.proto_objects[i].L) / 100)<< " X "
// << (((double)perception_output.proto_objects[i].W) / 100)<< " X "
// << (((double)perception_output.proto_objects[i].H) / 100)<< std::endl;
// }
// std::cout<< ((int)perception_output.proto_sys_status
// print out data end
// yuphe 20220801
arg->udp_output->UDPSocket::Send((char *)&perception_output, sizeof(perception_output));
arg->log->toFile((unsigned char *)&perception_output, sizeof(perception_output));
arg->m_LogFile->Start();//增加內(nèi)存檢測(cè) 20221102
// arg->outdata->callback_track_sub(*(arg->log), Sensor_state, System_state,my_version);
double process_time = double(clock() - time_start) / CLOCKS_PER_SEC * 100;
// ROS_ERROR_STREAM(" process_time......"<< process_time);
pthread_testcancel();
struct timeval tempval;
tempval.tv_sec = 0;
tempval.tv_usec = 100000; // 10hz
select(0, NULL, NULL, NULL, &tempval);
}
}
總結(jié)1.形式都是類似的,定義thread,然后在構(gòu)造函數(shù)或者pthread_creat函數(shù)中將要處理的函數(shù)及參數(shù)放進(jìn)去
2.pthread要放靜態(tài)函數(shù)。對(duì)于靜態(tài)函數(shù)的處理,尤其是對(duì)于類的非靜態(tài)成員函數(shù)的處理要留意,這部分后續(xù)補(bǔ)充。目前采用的辦法是thread th(&類名::函數(shù)名,對(duì)象地址,參數(shù))的方式。
3.之前做java的時(shí)候,好像是繼承Thread類,重寫run函數(shù),還有runable接口,好像在C+里面不怎么使用這種方式,后續(xù)遇到再補(bǔ)充。
幾篇博客:
C++多線程的實(shí)現(xiàn)
C++多線程詳細(xì)講解
C++ 多線程編程之在類中使用多線程(thread)的方法
C++多線程編程之thread類
C++ 在類里面使用多線程技術(shù)
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)頁(yè)標(biāo)題:C++thread的方式-創(chuàng)新互聯(lián)
文章位置:http://jinyejixie.com/article22/dphicc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司、自適應(yīng)網(wǎng)站、網(wǎng)站收錄、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容