C++thread的方式-創(chuàng)新互聯(lián)

多線程的實現(xiàn)方式,只做記錄,自己看。

站在用戶的角度思考問題,與客戶深入溝通,找到鶴壁網(wǎng)站設(shè)計與鶴壁網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站制作、成都網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名與空間、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋鶴壁地區(qū)。目錄
  • 第一種 在類中實現(xiàn)多線程
  • 第二種 在類外
  • 第三種 沒有類
  • 第四種 pthread,定時觸發(fā)
  • 總結(jié)
  • 附錄

第一種 在類中實現(xiàn)多線程

新建thread對象,傳入類的成員函數(shù)名稱,對象地址,以及成員函數(shù)需要的參數(shù)。

在類的成員函數(shù)中,要實現(xiàn)多線程
 void Perception::test()
 {.....
 		thread th( &Perception::showImage, this);  //實例化一個線程對象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,定時觸發(fā)

使用定時器觸發(fā),傳入多個參數(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);

其中,多個參數(shù)的定義在TIMECALL(結(jié)構(gòu)體),先對結(jié)構(gòu)體成員進行實例化、賦值,然后在將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)存檢測
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)存檢測
    // 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)存檢測 20221102 
    FileManage *m_RosLog = nullptr; //增加ROS日志檢測 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)存檢測 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ù)放進去
2.pthread要放靜態(tài)函數(shù)。對于靜態(tài)函數(shù)的處理,尤其是對于類的非靜態(tài)成員函數(shù)的處理要留意,這部分后續(xù)補充。目前采用的辦法是thread th(&類名::函數(shù)名,對象地址,參數(shù))的方式。
3.之前做java的時候,好像是繼承Thread類,重寫run函數(shù),還有runable接口,好像在C+里面不怎么使用這種方式,后續(xù)遇到再補充。

附錄

幾篇博客:
C++多線程的實現(xiàn)
C++多線程詳細講解
C++ 多線程編程之在類中使用多線程(thread)的方法
C++多線程編程之thread類
C++ 在類里面使用多線程技術(shù)

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)頁標題:C++thread的方式-創(chuàng)新互聯(lián)
文章位置:http://muchs.cn/article22/dphicc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化營銷型網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司、自適應(yīng)網(wǎng)站網(wǎng)站收錄、手機網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)