【C++】【multithreading】chapter2 managing threads

本文介绍了C++中线程的基本管理,包括通过函数或类实例创建线程,使用join和detach等待或分离线程,以及线程的移动操作。此外,还讨论了如何在运行时选择线程数量和识别线程ID。thread::hardware_concurrency()函数用于获取系统支持的最大线程数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 时间:2023年4月15日15点13分
// 知识点: 1. 线程的创建
//         2. 线程的join 和 detach 
//         3. 线程的move 、hardware_concurrency 和 get_id
1. Basic thread management
1.1 Launching a thread.

   有两种方式 launch a thread.
   1. 传函数进thread 对象中,创建 thread 对象。

void do_some_work();
std:thread my_thread (do_some_work);

   2. As with much of the C++ Standard Library, std::thread works with any callable type, so you can pass an instance of a class with a function call operator to the std::thread constructor.

class back_ground_task
{
public:
	void operator()() const // 此处重载了 function call operator.
	{
		do_something();
	}
};
back_ground_task f; // 创建f对象。
std::thread my_thread(f);
1.2 Waiting for a thread to finish vs leaving it to run.

   此处对应两种方式,join 和 detach。
   1. join
   在线程对象创建之后,调用 join 之前,如果程序出现了 exception ,那么程序就会被终止,join 就无法正常运行,所以要保证,join 在任何情况下都会被顺序执行到,即使出现了 exception 也要保证 join 可以正常运行。
   无论怎么样都要保证 join 的运行,此时想到的肯定是 try catch,但是书上原话说的是,The use of try/catch blocks is verbose, and it is easy to get the scope slightly wrong, so this isn’t an ideal scenario. 所以,我们选择的方法是把 join 函数写在 destructor 中。

// 这种方法的本质是把新创建的线程传到类中,所以在类被 destrcutor 后,join 会执行。
class thread_guard
{
	std::thread &t;
public:
	explicit thread_guard(std::thread& t_) t(t_){}
	~thread_guard()
	{
		if (t.joinable())
		{
			t.join();
		}
	}
	thread_guard(thread_guard const &) = delete;
	thread_guard & operator = (thread_guard const &) = delete;
};
void f()
{
	int some_local_state = 0;
	func my_func(some_local_state);
	std::thread t (my_func);
	thread_guard(t);// 把刚创建的线程传入到线程 guard 中。
}

   2. detach
   detach 不需要做这种处理,我也不知道detach 为什么不需要做。原文是:if you are detaching a thread, you can usually call detach() immediately after the thread has been started, so this isn’t a problem.

2. Passing arguments to a thread function

   直接在创建线程的时候,在后面加上变量即可。

void f(int i, std::string const & s);
std::thread t(f, 2, "hello");
3. Transfering ownership of a thread

   介绍了了线程的 move 用法。线程只能 move 不能copy。

void some_function();
void some_other_function();
std::thread t1(some_function);
std::thread t2 = std::move(t1);
t1 = std::thread (some_other_function);
std::thread t3;
t3 = std::move(t2);
t1 = std::move(t3);
4. Choosing the number of threads at runtime

   介绍了 hardware_concurrency(), 这个函数会返回 CPU 物理意义上支持的最大的线程数。

	auto concurrency_times = thread::hardware_concurrency();
	cout << concurrency_times << endl;
5. identifying threads

   介绍了下线程 id, 如果你想指定某个线程去运行某个程序,就可以用线程 id 来指定。

	thread t(hello);
	t.join();
	auto id = t.get_id();
	cout << id << endl;
参考文献:

   《C++ concurrency in action》

免责声明:

   本文素材来源网络,版权归原作者所有。如涉及作品版权问题,请与我联系删除。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值