C++ 将()数据从线程移回
我有一个类('Buffer'),它包含一个数据的C++ 将()数据从线程移回,c++,multithreading,move,shared-memory,unique-ptr,C++,Multithreading,Move,Shared Memory,Unique Ptr,我有一个类('Buffer'),它包含一个数据的,还有一个analyze()函数,它对数据进行一些计算('raw'和'optimized') 我的问题(我怀疑)是,在我将()我的对象移动到一个新线程以执行analyze()计算后,我无法单独从主线程访问其中的变量和方法(一旦并行线程完成) //from the main thread, once I know the parallel thread doing analyze() has finished queue[0]->someFun
//from the main thread, once I know the parallel thread doing analyze() has finished
queue[0]->someFunction() // CRASH! ERROR! BOOM!
PS:Here()是我如何设法使线程工作的参考,以及为什么它是唯一的解决方案。这不是真正的线程问题。这是对移动语义的误解,我认为,您已经将队列交给了分离的线程,因此不再拥有它 编辑(示例代码):
#包括
#包括
#包括
#包括
类元素
{
公众:
INTA;
浮动p;
元素(int _a,float _p=1.0):a(_a),p(_p){};
};
类缓冲区
{
公众:
缓冲区(){};
std::向量raw;
std::矢量优化;//基于原始数据计算的数据
void addElement(int _a,float _p=1.0)//填充原始缓冲区
{
原始。推回(元素(_a,_p));
}
void compute()//计算优化的缓冲区
{
浮动t;
int i;
对于(std::vector::iterator it=raw.begin();it!=raw.end();++it)
{
优化。推回(元素(it->a,it->p));
//做一些计算密集型的计算
std::cout您怎么知道“线程执行analyze()已完成”如果您分离它,而不是加入?请再次将其移回。完成后将其移回队列。@MartinJames我如何才能将其移回?我找不到方法将指向主线程的链接传递给调用线程,以便它可以将队列移回…嗯,我有点困惑:我在将对可调用对象的引用和对对象的引用传递给std::thread时想到了这些值不会被复制,只是从内存中的同一位置执行。Analyze()是缓冲区的成员函数,我作为第二个参数传递的是运行Analyze()函数的对象缓冲区。因此,为什么Analyze()中不需要任何参数……但我可能是一个混淆的参数,因此如果您能详细说明您的解决方案,请稍后再试(特别是带有代码的!)谢谢
std::unique_ptr<dynamica::Buffer> buffer; //TEMPORARY COLLECTOR
std::vector<std::unique_ptr<dynamica::Buffer>> queue; //COLLECTION OF ALL PREVIOUS BUFFERS
queue.push_back(move(buffer));
//RUN 'ANALYZE' IN PARALLEL IN BACKGROUND
std::thread t(&Buffer::analyze, move(queue.back()));
t.detach();
//from the main thread, once I know the parallel thread doing analyze() has finished
queue[0]->someFunction() // CRASH! ERROR! BOOM!
#include <vector>
#include <iostream>
#include <memory>
#include <thread>
class Element
{
public:
int a;
float p;
Element(int _a, float _p=1.0): a(_a), p(_p){};
};
class Buffer
{
public:
Buffer(){};
std::vector<Element> raw;
std::vector<Element> optimised; // DATA CALCULATED BASED ON RAW
void addElement(int _a,float _p=1.0) // FILL THE RAW BUFFER
{
raw.push_back(Element(_a,_p));
}
void compute() // COMPUTE THE OPTIMISED BUFFER
{
float t;
int i;
for(std::vector<Element>::iterator it = raw.begin(); it != raw.end(); ++it)
{
optimised.push_back(Element(it->a,it->p));
// DO SOME COMPUTATIONALLY INTENSIVE CALCULATIONS
std::cout << "Performing calculations..." << std::endl;
for(i=1; i<9999999; i++)
t = 9./i;
}
};
void clear() // ERASE BOTH BUFFERS
{
raw.clear();
optimised.clear();
}
const std::pair<std::vector<Element>::const_iterator, std::vector<Element>::const_iterator> someFunction()
{
return std::make_pair<std::vector<Element>::const_iterator, std::vector<Element>::const_iterator>(begin(optimised), end(optimised));
}
~Buffer() { clear(); }
};
int _tmain(int argc, _TCHAR* argv[])
{
Buffer origin;
auto buffer = std::make_shared<Buffer *>(&origin);
std::vector<decltype(buffer)> queue;
for(int i = 0; i < 10; i++ )
(*buffer)->addElement(i);
queue.push_back(buffer);
std::thread t(&Buffer::compute, *queue.back());
t.join(); // wait for results
auto result = (*queue.back())->someFunction();
for( auto ele = result.first; ele != result.second; ele++ )
{
std::cout << (*ele).a << std::endl;
}
return 0;
}