小编Joh*_*Doe的帖子

boost:偶尔同步任务的asio线程池实现

我有一个"主"功能,每个时间步一次执行许多小的,独立的任务.但是,在每个时间步之后,我必须等待所有任务完成才能前进.

我想让程序多线程化.我已尝试使用boost-offshoot线程池实现,我尝试使用(共享指针)线程的向量,我尝试了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(); 
}
Run Code Online (Sandbox Code Playgroud)

这是我所拥有的(但不知道如何实现):

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, …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading gcc boost boost-asio

11
推荐指数
1
解决办法
2728
查看次数

你如何在C++ 03中将boost packaged_task发布到io_service?

这是前一个问题(这里)的后续内容,但我正在研究一个多线程应用程序,我想将一个Boost packaged_task发布到一个线程化的io_service.我坚持使用C++ 03编译器(所以std :: move已经出局),并且packaged_task不可复制.我已经尝试将它包装在shared_ptr中并传递它以及许多其他内容.这是我当前的尝试和后续的编译器错误.知道如何让这个工作吗?

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));
}
std::vector<boost::shared_future<bool> > pending_data; // vector of futures

bool process_data(int,int){...}
...

for(int theTime = 0; theTime != totalScenarioTime; ++theTime)
{
    for(int i = 0; i < numSmallTasks; ++i)
    {
        boost::packaged_task<bool> task(boost::bind(&process_data,i,theTime));
        boost::shared_future<bool> fut(task.get_future());
        pending_data.push_back(fut); // C++11 possible: (std::move(fut) when fut is a unique_future);
        io_service.post(task); // C++11 possible: (std::move(task));    
    }
    // After loop - wait until …
Run Code Online (Sandbox Code Playgroud)

multithreading boost future boost-asio

11
推荐指数
1
解决办法
3026
查看次数

为什么我可以像标量矩阵一样初始化常规的Boost矩阵?

为什么这样做?它不在任何地方的文档中......

#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

int main()
{

boost::numeric::ublas::matrix<double> twoByTwoMat(2,2,-2);

std::cout << "This is the matrix: " << twoByTwoMat << std::endl;

return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

This is the matrix: [2,2]((-2,-2),(-2,-2))
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-ublas

3
推荐指数
1
解决办法
1611
查看次数

标签 统计

boost ×3

boost-asio ×2

c++ ×2

multithreading ×2

boost-ublas ×1

future ×1

gcc ×1