小编nru*_*dle的帖子

Boost.Thread没有加速?

我有一个小程序,使用各种卡计数策略实现BlackJack的蒙特卡罗模拟.我的主要功能基本上是这样的:

int bankroll = 50000;
int hands = 100;
int tests = 10000;
Simulation::strategy = hi_lo;

for(int i = 0; i < simulations; ++i)
   runSimulation(bankroll, hands, tests, strategy);
Run Code Online (Sandbox Code Playgroud)

整个程序在我的机器上的单个线程中运行大约需要10秒钟.

我想利用我的处理器所拥有的3个内核,所以我决定重写程序,只需在不同的线程中执行各种策略,如下所示:

int bankroll = 50000;
int hands = 100;
int tests = 10000;
Simulation::strategy = hi_lo;
boost::thread threads[simulations];

for(int i = 0; i < simulations; ++i)
   threads[i] = boost::thread(boost::bind(runSimulation, bankroll, hands, tests, strategy));

for(int i = 0; i < simulations; ++i)
   threads[i].join();
Run Code Online (Sandbox Code Playgroud)

但是,当我运行此程序时,即使我得到相同的结果,也需要大约24秒才能完成.我在这里错过了什么吗?

c++ boost-thread

2
推荐指数
1
解决办法
273
查看次数

标签 统计

boost-thread ×1

c++ ×1