c++patterns
Create a thread
123456789101112131415161718 | #include <thread>#include <string>#include <functional>void func(std::string str, int& x);void do_something();int main(){ std::string str = "Test"; int x = 5; std::thread t{func, str, std::ref(x)}; do_something(); t.join();} |
This pattern is licensed under the CC0 Public Domain Dedication.
本文探讨了在C++中使用标准库thread进行线程创建的模式,通过一个实例展示了如何在主线程中启动并等待子线程完成,同时传递参数给线程函数。

被折叠的 条评论
为什么被折叠?



