io_service线程安全队列效率

梦想游戏人
目录:
C/C++

boost::asio::io_service 的本质是一个消费者模型 多线程环境下 效率测试

空转的话效率大概是150W QPS,  模拟find操作的话是80W QPS,

std::map<int, int> _x;
int QPS_WRITE = 0;
io_service io;
int main(int argc, char* argv[])
{
	for (int i = 0; i < 10000; i++)
	{
		_x[i] = i;
	}
	for (int i = 0; i < 50; i++)
	{
		std::thread t([&]()
		{
			while (true)
			{
				this_thread::sleep_for(std::chrono::nanoseconds(1));
				for (int i = 0; i < 16; i++)
					io.post([&]()
				{
					++QPS_WRITE;
					for (int ii = 0; ii < 10; ii++)
						auto x = _x.find(ii);
				});
			}
		});
		t.detach();
	}

	std::thread t([&]()
	{
		while (true)
		{
			this_thread::sleep_for(std::chrono::seconds(1));
			cout << QPS_WRITE << endl;
			QPS_WRITE = 0;
		}
	});
	t.detach();


	io_service::work work(io);
	io.run();

	::system("pause");
	return 0;
}
Scroll Up