CPP: how to use non-static member function as a callback of a thread?

Share:
## 目标 - 如何在类中使用非静态成员函数创建线程 ## code ```cpp #include #include #include class foo { public: void make_foo_func_threads() { for (int i = 0; i < 5; ++i) some_threads.push_back(std::thread(&foo::foo_func, this)); for (auto& t : some_threads) t.join(); } private: void foo_func() { std::cout << "Hello"< some_threads; }; int main() { foo f; f.make_foo_func_threads(); } ``` ## Result ```cpp ./thread_in_class Hello Hello Hello Hello Hello ``` ## Reference - https://stackoverflow.com/questions/26992834/setting-a-callback-function-which-is-non-static-member-function-of-a-class ![yubao_blog_cover](https://raw.githubusercontent.com/yubaoliu/assets/image/yubao_blog_cover.png)

No comments