gpt4 book ai didi

c++ - boost group_threads 最大并行线程数

转载 作者:太空狗 更新时间:2023-10-29 21:00:12 26 4
gpt4 key购买 nike

我想在我的程序中使用最大数量的线程应用 boost group_thread。例如

int maxNumberOfThreads
boost::thread_group group;
for (int i = 0; i < N; ++i)
//create new if group.size() is smaller then maximal number of threads
group.create_thread(Worker);
group.join_all();

有人知道我如何实现这一点吗?

因为当我启动N个线程时效率会很低。

谢谢你的帮助

最佳答案

你似乎想要的是一个线程池。

您可以使用 boost::thread::hardware_concurrency() 来确定特定系统上可用的(逻辑)核心数。

这是我上周滚动的答案:

#include <boost/thread.hpp>
#include <boost/phoenix.hpp>
#include <boost/optional.hpp>

using namespace boost;
using namespace boost::phoenix::arg_names;

boost::atomic_size_t counter(0ul);

class thread_pool
{
private:
mutex mx;
condition_variable cv;

typedef function<void()> job_t;
std::deque<job_t> _queue;

thread_group pool;

boost::atomic_bool shutdown;
static void worker_thread(thread_pool& q)
{
while (optional<job_t> job = q.dequeue())
(*job)();
}

public:
thread_pool() : shutdown(false) {
for (unsigned i = 0; i < boost::thread::hardware_concurrency(); ++i)
pool.create_thread(bind(worker_thread, ref(*this)));
}

void enqueue(job_t job)
{
lock_guard<mutex> lk(mx);
_queue.push_back(job);

cv.notify_one();
}

optional<job_t> dequeue()
{
unique_lock<mutex> lk(mx);
namespace phx = boost::phoenix;

cv.wait(lk, phx::ref(shutdown) || !phx::empty(phx::ref(_queue)));

if (_queue.empty())
return none;

job_t job = _queue.front();
_queue.pop_front();

return job;
}

~thread_pool()
{
shutdown = true;
{
lock_guard<mutex> lk(mx);
cv.notify_all();
}

pool.join_all();
}
};

一个典型的使用方法也在那个答案中:

static const size_t bignumber = 1 << 20;

class myClass
{
thread_pool pool; // uses 1 thread per core

public:
void launch_jobs()
{
std::cout << "enqueuing jobs... " << std::flush;
for(size_t i=0; i<bignumber; ++i)
{
for(int j=0; j<2; ++j) {
pool.enqueue(bind(&myClass::myFunction, this, j, i));
}
}
std::cout << "done\n";
}

private:
void myFunction(int i, int j)
{
boost::this_thread::sleep_for(boost::chrono::milliseconds(1));
counter += 1;
}
};

int main()
{
myClass instance;
instance.launch_jobs();

size_t last = 0;
while (counter < (2*bignumber))
{
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
if ((counter >> 4u) > last)
{
std::cout << "Progress: " << counter << "/" << (bignumber*2) << "\n";
last = counter >> 4u;
}
}
}

对于那个问题,在另一个答案的评论中,我还发布了一个基于无锁作业队列实现的等效解决方案:

关于c++ - boost group_threads 最大并行线程数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22685176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com