C++11 std::thread在类的成员函数中的使用

Posted on 2019-12-18 09:01  yacbo  阅读(7445)  评论(2编辑  收藏  举报

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; }

 

Copyright © 2024 yacbo
Powered by .NET 8.0 on Kubernetes