Class 3 thread start, end, create a multi-thread method, join, detach

With class creates a thread as a callable object

class A
{
public:
    A() { cout << "构造函数" << endl; }
    A(const A& a) { cout << "拷贝构造函数" << endl; }

    void operator()() { cout << "此时在子线程中" << endl; }

    ~A() { cout << "析构函数" << endl; }
private:

};

int main(int argc, char** argv)
{
    A a;
    thread myjob(a);

    myjob.join();

    cout << "此时在主线程中" << endl;
    return 0;
}

Create a thread with lambda as a callable object

int main(int argc, char** argv)
{
    auto f = [] {cout << "lambda" << endl; };
    thread myjob(f);
    //thread myjob([] {cout << "lambda" << endl; });这样的使用方法也可以通过编译

    myjob.join();

    cout << "此时在主线程中" << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/Anthony-ling/p/11441185.html