【C++编程】使用lambda创建线程

使用lambda创建线程

1. 例子:

#include <iostream>
#include <thread>
#include <chrono>
#include <vector>
#include <mutex>
#include <memory>

class Task
{
public:
	Task(int x, std::shared_ptr<std::mutex> mutex) : _x(x), _mutex(mutex) {}

	void handle()
	{
		int task_id = 0;
		while (true) {
			if (_x > 0) {
				std::lock_guard<std::mutex> lock(*_mutex);
				if (_x > 0) {
					task_id = _x;
					--_x;
				} else {
					_x = 0;
				}
			// do task
			std::cout << "do task id: " << task_id << std::endl;
			} else {
				return;
			}

			std::this_thread::sleep_for(std::chrono::seconds(1));
		}
	}

private:
	int _x;
	std::shared_ptr<std::mutex> _mutex;
};

int main()
{
	int x = 0;
	const int THREAD_NUM = 7;
	const int TASK_NUM = 100;
	std::vector<std::thread> threads;

	std::shared_ptr<std::mutex> mutex = std::make_shared<std::mutex>();
	Task t(TASK_NUM, mutex);

	// 新建线程, std::thread支持使用lambda
	for (int i = 0; i < THREAD_NUM; ++i) {
		threads.emplace_back(std::thread([&t] { t.handle(); }));
	}

	// 等待线程结束
	for (auto &thread : threads) {
		thread.join();
	}
	return 0;
}

c++ 之 std::this_thread::yield 与std::this_thread::sleep_for

 

posted @ 2019-01-13 19:51  苏格拉底的落泪  阅读(625)  评论(0编辑  收藏  举报