可变参数应用(C++并发编程中的joining_thread代码)
- 电脑硬件
- 2025-07-21 18:54:24

代码: #include "X:\Work\Share\CCode\CPlatform\Base\global_c_all.h" using namespace lf; using namespace std; class joining_thread { std::thread t; public: joining_thread() noexcept = default; template<typename Callable, typename ... Args> explicit joining_thread(Callable&& func, Args&& ... args) : t(std::forward<Callable>(func), std::forward<Args>(args)...) { } explicit joining_thread(std::thread t_) noexcept : t(std::move(t_)) { } joining_thread(joining_thread&& other) noexcept : t(std::move(other.t)) { } joining_thread& operator=(joining_thread&& other) noexcept { if (joinable()) join(); t = std::move(other.t); return *this; } ~joining_thread() noexcept { if (joinable()) join(); } void swap(joining_thread& other)noexcept { t.swap(other.t); } std::thread::id get_id() const noexcept { return t.get_id(); } bool joinable() const noexcept { return t.joinable(); } void join() { t.join(); } void detach() { t.detach(); } std::thread& as_thread() noexcept { return t; } const std::thread& as_thread() const noexcept { return t; } }; int main() { joining_thread t(std::thread([]() { _cout << _t("执行线程代码。"); })); //t.join(); return 0; } 输出:
可变参数应用(C++并发编程中的joining_thread代码)由讯客互联电脑硬件栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“可变参数应用(C++并发编程中的joining_thread代码)”