当前位置: 首页 > news >正文

可变参数应用(C++并发编程中的joining_thread代码)

代码:

#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;
}
 

输出:

相关文章:

  • 解决Android AAPT: error: resource android:attr/lStar not found. 问题
  • CMake入门教程【基础篇】列表操作(list)
  • 超简单|配图详细| 双系统Ubuntu22.04 系统磁盘扩容
  • 旅游平台网页前后端
  • Selenium教程04:鼠标+键盘网页的模拟操作
  • 基于SpringBoot的物流管理系统
  • linux系统shell流文本处理工具——awk
  • 使用IDEA官方docker插件构建镜像
  • 速学python·注释
  • openssl 命令详解
  • MySQL的CRUD操作函数介绍union和union all
  • 2023年终总结
  • Elasticsearch:使用 ELSER v2 文本扩展进行语义搜索
  • RoadMap8:C++中类的封装、继承、多态与构造函数
  • 宣传照(私密)勿转发
  • 使用.Net nanoFramework为ESP32进行蓝牙配网
  • Django 文件上传(十二)
  • prometheus grafana mysql监控配置使用
  • 【JAVA】泛型和Object的区别
  • 深入了解Swagger注解:@ApiModel和@ApiModelProperty实用指南
  • 解放日报:“感觉全世界人都在上海买买买”
  • 世界高血压日|专家:高血压患者控制血压同时应注重心率管理
  • “GoFun出行”订单时隔7年扣费后续:平台将退费,双方已和解
  • 铁路端午假期运输火车票今日开售,12306提升应对超大规模并发访问需求能力
  • 鸿海下调全年营收展望:AI服务器业务强劲,预计今年营收增超50%
  • 陕西旱情实探:大型灌区农业供水有保障,大旱之年无旱象