C++11 std::thread在類的成員函數中的使用


1.

c++類成員函數作為回調函數

thread在類的成員函數中的使用

#include <thread>
#include <iostream>
using namespace std;
class Wrapper { public: void member1() { cout << "member1" << endl; }
void member2(const char *arg1, unsigned arg2) { cout << "i am member2 and my first arg is ("
         << arg1 << ") and second arg is ("
         << arg2 << ")" <<endl; }
std::thread member1Thread() {
return thread(&Wrapper::member1, this); }
std::thread member2Thread(
const char *arg1, unsigned arg2) { return thread(&Wrapper::member2, this, arg1, arg2); } }; int main() { Wrapper *w = new Wrapper(); std::thread tw1 = w->member1Thread(); tw1.join(); w->member2Thread("hello", 100).detach(); return 0; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM