可变参数应用(C++并发编程中的joining_thread代码)
发布时间:2024年01月06日
代码:
#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;
}
输出:
文章来源:https://blog.csdn.net/weixin_42944928/article/details/135426222
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:chenni525@qq.com进行投诉反馈,一经查实,立即删除!