序列化部分工作 - boost::asio

发布于 2024-11-02 20:05:42 字数 1817 浏览 0 评论 0原文

    void wt(shared_ptr<io_service> io_servicee)
{
    io_servicee->run();
}

void calculateAndReturn(int myNumber, shared_ptr<vector<int> > vectorOfResults)
{
    /*
     * Here will be a lot of pararel computing (first part)...
     */

    int result = myNumber;

    /* This part (second part) below I want to be executed sequentially depending on the number of thread.
     * Thread which has myNumber == 1 should be first, second should be thread with
     * myNumber == 2, third with myNumber == 3 etc.
     *
     */
    mt.lock();
    vectorOfResults->push_back(result);
    mt.unlock();
}

int main()
{
    shared_ptr< io_service > io_servicee(new io_service);
    shared_ptr< io_service::work > work(new io_service::work( *io_servicee ));
    shared_ptr<vector<int> > vectorOfSums(new vector<int>);
    thread_group worker_threads;


    for( int x = 0; x < 4; ++x )
    {
            worker_threads.create_thread(bind( &wt, io_servicee ));
    }

    for( int x = 0; x < 4; ++x )
    {

        io_servicee->post(bind( &calculateAndReturn, x, vectorOfSums));
    }
    work.reset();
    worker_threads.join_all();

    for ( int i = 0; i < vectorOfSums->size(); i++)
    {
        cout << (*vectorOfSums)[i] << endl;

    }

}

我想序列化calculateAndReturn函数的一部分。它应该是这样的:

First part of thread 1 ------->  \ 
                                  \
First part of thread 2 -------> -- Second part of thread 1 --> second part of thread2...
                                  /
     ....                        /
                                /
First part of thread n ------->/

我不想在主函数内部使用链,而是在calculateAndReturn的第二部分之前使用strand。有什么优雅的解决方案吗?

此致

    void wt(shared_ptr<io_service> io_servicee)
{
    io_servicee->run();
}

void calculateAndReturn(int myNumber, shared_ptr<vector<int> > vectorOfResults)
{
    /*
     * Here will be a lot of pararel computing (first part)...
     */

    int result = myNumber;

    /* This part (second part) below I want to be executed sequentially depending on the number of thread.
     * Thread which has myNumber == 1 should be first, second should be thread with
     * myNumber == 2, third with myNumber == 3 etc.
     *
     */
    mt.lock();
    vectorOfResults->push_back(result);
    mt.unlock();
}

int main()
{
    shared_ptr< io_service > io_servicee(new io_service);
    shared_ptr< io_service::work > work(new io_service::work( *io_servicee ));
    shared_ptr<vector<int> > vectorOfSums(new vector<int>);
    thread_group worker_threads;


    for( int x = 0; x < 4; ++x )
    {
            worker_threads.create_thread(bind( &wt, io_servicee ));
    }

    for( int x = 0; x < 4; ++x )
    {

        io_servicee->post(bind( &calculateAndReturn, x, vectorOfSums));
    }
    work.reset();
    worker_threads.join_all();

    for ( int i = 0; i < vectorOfSums->size(); i++)
    {
        cout << (*vectorOfSums)[i] << endl;

    }

}

I want to serialize part of calculateAndReturn function. It should goes like this:

First part of thread 1 ------->  \ 
                                  \
First part of thread 2 -------> -- Second part of thread 1 --> second part of thread2...
                                  /
     ....                        /
                                /
First part of thread n ------->/

I dont want to use strand inside main function, rather before second part in calculateAndReturn. Is there any elegant solution for it?

Best regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒尘 2024-11-09 20:05:42

如果您在单个进程中执行此操作,我建议使用 OpenMP。规范示例:

#include <omp.h>
#define N 1000
#define CHUNKSIZE  100

main ()  
{
    int i, chunk;
    float a[N], b[N], c[N];

    /* Some initializations */
    for (i=0; i < N; i++)
      a[i] = b[i] = i * 1.0;
    chunk = CHUNKSIZE;

    #pragma omp parallel for shared(a,b,c,chunk) private(i) schedule(static,chunk)
    for (i=0; i < n; i++)
        c[i] = a[i] + b[i];
}

在 gnu 上:安装 libgomp,构建

g++ -fopenmp -lgomp source.cpp

If you're doing this in a single process, I suggest using OpenMP. Canonical example:

#include <omp.h>
#define N 1000
#define CHUNKSIZE  100

main ()  
{
    int i, chunk;
    float a[N], b[N], c[N];

    /* Some initializations */
    for (i=0; i < N; i++)
      a[i] = b[i] = i * 1.0;
    chunk = CHUNKSIZE;

    #pragma omp parallel for shared(a,b,c,chunk) private(i) schedule(static,chunk)
    for (i=0; i < n; i++)
        c[i] = a[i] + b[i];
}

On gnu: install libgomp, build

g++ -fopenmp -lgomp source.cpp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文