Thread参数传递问题

时间:2025-05-09 09:46:06

一、类的普通成员函数作为Thread的参数

class threadtest
{
private:

public:
    threadtest() { }
    ~threadtest() { }
    // 类的普通成员函数
    void test_fun1(int num) {
        for (int i = 0; i < num; i++)
            cout << "thread test1" << endl;
        return;
    }
};
threadtest tt;
thread th1(&threadtest::test_fun1, &tt, 10);
th1.join();

二、类的静态成员函数作为Thread的参数

class threadtest
{
private:

public:
    threadtest() { }
    ~threadtest() { }
    // 类的静态成员函数
    static void test_fun2(string str) {
        for(int i = 0; i < 10; i++) {
            cout << str << endl;
        }
    }
};
threadtest tt;
thread th2(&threadtest::test_fun2, "thread test2");
th2.join();

三、普通函数作为Thread的参数

// 普通函数
void test_fun3(string str) {
    for(int i = 0; i < 10; i++) {
        cout << str << endl;
    }
}
thread th3(test_fun3, "thread test3");
th3.join();