提高:对于偶尔同步任务ASIO线程池实现(boost:asio thread pool implem

2019-08-02 03:43发布

我有每个时间步骤执行许多小的,独立的任务每一次“主”的功能。 然而,每一个时间步之后,我必须等待所有的任务挺身而出之前完成。

我想使程序多线程。 我曾尝试与升压分支线程池的实现,我已经使用(共享指针)线程矢量尝试过了,我已经试过了ASIO线程池的想法(使用io_service对象,建立了一些工作,然后分发运行到线程和张贴处理程序io_service对象)。

所有这些似乎有很多的开销创建和我的销毁线程的“很多小的任务,”我想一个办法,最好使用ASIO工具,实例化一个io_service对象,一个thread_group,张贴处理程序io_service对象,并等待单个时间步长的工作发布更多的任务前完成。 有没有做到这一点的好办法? 这里是(精简)的代码为我现在的工作:

boost::asio::io_service io_service;
for(int theTime = 0; theTime != totalTime; ++theTime)
{
    io_service.reset();
    boost::thread_group threads;
    // scoping to destroy the work object after work is finished being assigned
    {
        boost::asio::io_service::work work(io_service);
        for (int i = 0; i < maxNumThreads; ++i)
        {
            threads.create_thread(boost::bind(&boost::asio::io_service::run,
                &io_service));
        }

        for(int i = 0; i < numSmallTasks; ++i)
        {
            io_service.post(boost::bind(&process_data, i, theTime));
        }
    }
    threads.join_all(); 
}

这就是我宁愿有(但不知道如何实现):

boost::asio::io_service io_service;
boost::thread_group threads;
boost::asio::io_service::work work(io_service);
for (int i = 0; i < maxNumThreads; ++i)
{
    threads.create_thread(boost::bind(&boost::asio::io_service::run,
         &io_service));
}

for(int theTime = 0; theTime != totalTime; ++theTime)
{
    for(int i = 0; i < numSmallTasks; ++i)
    {
        io_service.post(boost::bind(&process_data, i, theTime));
    }
    // wait here until all of these tasks are finished before looping 
    // **** how do I do this? *****
}
// destroy work later and join all threads later...

Answer 1:

您可以使用期货进行数据处理,并使用与他们同步boost::wait_for_all() 这将允许你在做的部分工作,而不是线程方面工作。

int process_data() {...}

// Pending futures
std::vector<boost::unique_future<int>> pending_data;

for(int i = 0; i < numSmallTasks; ++i)
{
   // Create task and corresponding future
   // Using shared ptr and binding operator() trick because
   // packaged_task is non-copyable, but asio::io_service::post requires argument to be copyable

   // Boost 1.51 syntax
   // For Boost 1.53+ or C++11 std::packaged_task shall be boost::packaged_task<int()>
   typedef boost::packaged_task<int> task_t;

   boost::shared_ptr<task_t> task = boost::make_shared<task_t>(
      boost::bind(&process_data, i, theTime));

   boost::unique_future<int> fut = task->get_future();

   pending_data.push_back(std::move(fut));
   io_service.post(boost::bind(&task_t::operator(), task));    
}

// After loop - wait until all futures are evaluated
boost::wait_for_all(pending_data.begin(), pending_data.end()); 


Answer 2:

可能是你可以使用的boost ::障碍如下:

void thread_proc( boost::barrier& b ) {
    while( true ) {
        if( !ioservice.run_one() ) break; // io_service stopped
        b.wait();
    }
}


Answer 3:

罗斯特的方法主要工作,但的boost :: make_shared不能编译原样。 下面是一个工作版本(在vs2012):

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/make_shared.hpp>
#include <boost/function_types/result_type.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <boost/thread.hpp>

std::vector<boost::unique_future<void>> pending_data;
typedef boost::packaged_task<void> task_t;

boost::shared_ptr< boost::packaged_task<void> > pt(new boost::packaged_task<void> ([&,i](){...}));
boost::unique_future<void> result = pt->get_future();
pending_data.push_back(boost::move(result));
io_service.post(boost::bind(&task_t::operator(), pt));

boost::wait_for_all(pending_data.begin(), pending_data.end()); 
pending_data.clear();

它不会编译在packaged_task的typedef,如果使用的说法。 此线程通过ASIO和未来的方法游泳池只有每个循环创建新线程的方法相比节省8%的时间。



文章来源: boost:asio thread pool implementation for occasionally synchronized tasks