亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

想問一下,怎樣在自定義類中的函數使用thread?

想問一下,怎樣在自定義類中的函數使用thread?

蝴蝶不菲 2022-07-15 13:09:44
#include <iostream>#include <thread>using namespace std;class test{public:void output(const int a){cout << a << endl;}void work(const int a){thread th(output,a);th.join();}};int main(){test t;t.work(10);/******or/thread th(t.work,10);/th.join();*******/return 0;}寫個程序試一下,想在自定義的類的函數中使用thread或者把自定義的類的函數作為thread的驅動函數,但是上面兩種寫法都不行。/******or/thread th(t.output,10);/th.join();*******/注釋里面應該是這樣
查看完整描述

2 回答

?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

另一種是把 output 聲明為靜態成員函數


class test{public:    static void output(const int a)    {        cout << a << endl;    }    void work(const int a)    {        thread th(output,a);        th.join();    }};


查看完整回答
反對 回復 2022-07-18
?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

thread th( &test::output, this, a);

查看完整回答
反對 回復 2022-07-18
  • 2 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號