0

我有一个多线程应用程序。每个线程在其自己的本地存储中初始化一个结构数据类型。一些元素正被添加到结构类型变量中的向量中。在程序结束时,我想遍历这些线程本地存储并将所有结果添加到一起。我怎样才能遍历线程特定的指针,以便我可以将多线程中的所有结果一起添加?如何遍历提升线程特定指针

在此先感谢。

boost::thread_specific_ptr<testStruct> tss; 

size_t x = 10; 

void callable(string str, int x) { 
    if(!tss.get()){ 
     tss.reset(new testStruct); 
     (*tss).xInt.resize(x, 0); 
    } 
    // Assign some values to the vector elements after doing some calculations 
} 

例子:

#include <iostream> 
#include <vector> 
#include <boost/thread/mutex.hpp> 
#include <boost/thread/tss.hpp> 
#include <boost/thread.hpp> 
#include <boost/asio.hpp> 
#include <boost/bind.hpp> 

#define NR_THREAD 4 
#define SAMPLE_SIZE 500 

using namespace std; 

static bool busy = false; 

struct testStruct{ 
    vector<int> intVector; 
}; 

boost::asio::io_service ioService; 
boost::thread_specific_ptr<testStruct> tsp; 
boost::condition_variable cond; 
boost::mutex mut; 

void callable(int x) { 
    if(!tsp.get()){ 
     tsp.reset(new testStruct); 
    } 

    (*tsp).intVector.push_back(x); 

    if (x + 1 == SAMPLE_SIZE){ 
     busy = true; 
     cond.notify_all(); 
    } 
} 

int main() { 
    boost::thread_group threads; 
    size_t (boost::asio::io_service::*run)() = &boost::asio::io_service::run; 
    boost::asio::io_service::work work(ioService); 

    for (short int i = 0; i < NR_THREAD; ++i) { 
     threads.create_thread(boost::bind(run, &ioService)); 
    } 

    size_t iterations = 10; 
    for (int i = 0; i < iterations; i++) { 
     busy = false; 

     for (short int j = 0; j < SAMPLE_SIZE; ++j) { 
      ioService.post(boost::bind(callable, j)); 
     } 

     // all threads need to finish the job for the next iteration 
     boost::unique_lock<boost::mutex> lock(mut); 
     while (!busy) { 
      cond.wait(lock); 
     } 
     cout << "Iteration: " << i << endl; 
    } 

    vector<int> sum(SAMPLE_SIZE, 0); // sum up all the values from thread local storages 

    work.~work(); 
    threads.join_all(); 

    return 0; 
} 
+0

你只需自己编写该逻辑即可。看起来像一个普通的整合步骤。使函数返回非空,并将期货的价值相加,例如 – sehe

+0

每个线程在N不固定的情况下调用该函数N次,并且可能高达数百万次。比方说,我有8个线程,每个线程得到约100万个工作,我认为在这种情况下返回值不是一个好的解决方案。 – serhatg

+0

为什么不呢?只需返回否则需要从TLS获得的值。或者,如果您不想/不需要加入该线程,则使用承诺。 – sehe

回答

0

所以,以后我还给出了一些思考这个问题,我想出了这样的解决方案:

void accumulateTLS(size_t idxThread){ 

    if (idxThread == nr_threads) // Suspend all the threads till all of them are called and waiting here 
    { 
     busy = true; 
    } 

    boost::unique_lock<boost::mutex> lock(mut); 
    while (!busy) 
    { 
     cond.wait(lock); 
    } 

    // Accumulate the variables using thread specific pointer 

    cond.notify_one(); 
} 

有增强io_service对象,可调用函数可以在线程初始化后更改。因此,在完成所有计算后,我使用可调用函数accumulateTLS(idxThread)将作业(与线程数一样多)再次发送到io服务。 N个作业发送到N个线程,累积过程在accumulateTLS方法内完成。

P.S.而不是工作。〜work(),应该使用work.reset()。