C++ 如何遍历特定于boost线程的指针
我有一个多线程应用程序。每个线程在自己的本地存储中初始化一个结构数据类型。一些元素被添加到结构类型变量内的向量中。在程序结束时,我希望遍历这些线程本地存储,并将所有结果添加到一起。如何迭代线程特定指针,以便将多线程的所有结果添加到一起 提前谢谢C++ 如何遍历特定于boost线程的指针,c++,multithreading,boost-thread,thread-local-storage,thread-specific-storage,C++,Multithreading,Boost Thread,Thread Local Storage,Thread Specific Storage,我有一个多线程应用程序。每个线程在自己的本地存储中初始化一个结构数据类型。一些元素被添加到结构类型变量内的向量中。在程序结束时,我希望遍历这些线程本地存储,并将所有结果添加到一起。如何迭代线程特定指针,以便将多线程的所有结果添加到一起 提前谢谢 boost::thread_specific_ptr<testStruct> tss; size_t x = 10; void callable(string str, int x) { if(!tss.get()){
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
}
boost::特定于线程的ptr tss;
尺寸x=10;
void可调用(字符串str,int x){
如果(!tss.get()){
重置tss(新的testStruct);
(*tss.xInt.resize(x,0);
}
//完成一些计算后,为向量元素指定一些值
}
例如:
#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义NR_线程4
#定义样本大小500
使用名称空间std;
静态布尔忙=假;
结构测试结构{
矢量积分矢量;
};
boost::asio::io_服务ioService;
boost::特定于线程的ptr tsp;
boost::条件变量cond;
mutexmut;
void可调用(int x){
如果(!tsp.get()){
重置(新的testStruct);
}
(*tsp).intVector.push_-back(x);
如果(x+1==样本大小){
忙=真;
条件通知所有人();
}
}
int main(){
boost::线程组线程;
大小(boost::asio::io_服务::*运行)(=&boost::asio::io_服务::运行;
boost::asio::io_service::work-work(ioService);
对于(短整数i=0;i cout所以,在我考虑了这个问题之后,我想出了这样一个解决方案:
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();
}
void累加器(size\t idxThread){
if(idxThread==nr_threads)//挂起所有线程,直到所有线程都被调用并在此处等待
{
忙=真;
}
boost::唯一锁定(mut);
当(!忙)
{
等待(锁定);
}
//使用特定于线程的指针累积变量
第二,通知某人;
}
使用boost io_服务,可以在线程初始化后更改可调用函数。因此,在完成所有计算后,我将使用可调用函数AccumerateTLS(idxThread)再次向io服务发送作业(与线程数相同)。N个作业被发送到N个线程,累加过程在AccumerateTLS方法内完成
注意,不要工作。~work(),work.reset()应该使用。您只需自己编写该逻辑。看起来像是一个普通的整合步骤。让函数返回非void,然后对未来值求和,例如,每个线程调用函数N次,其中N不是固定的,可以达到数百万次。假设我有8个线程,每个线程获得~1m个作业,我认为这是正确的在这种情况下,返回值不是一个好的解决方案。为什么不呢?只返回需要从TLS获取的值。或者,如果不想/不需要加入线程,则使用承诺。好的,主要思想是提高性能,这就是为什么我不希望在每次迭代后返回该值,而是在I中进行计算n将每个线程分开,最后将几个向量加在一个内核中。所有的线程在所有迭代完成后都会连接起来。嗯。只是重复你的假设并不会使它们变得更有用。我明白你的意思。我试图告诉你你找错了树。做一个SSCE,我会给你看。(如果需要,可以使用多个版本)