简体   繁体   English

在std :: thread中使用std :: vector

[英]Using std::vector with std::thread

I have a program where I go through each class in a std::vector , do some operations on it and write it to a new std::vector 我有一个程序,可以在std :: vector中遍历每个类,对其进行一些操作,然后将其写入新的std :: vector

In my program the std::vector is large and the operations done on the classes are time consuming . 在我的程序中,std :: vector很大,在类上执行的操作很耗时。 So I was wondering if I could use std::thread to break up the operation on std::vector into chunks . 所以我想知道是否可以使用std :: thread将std :: vector上的操作分解为块。 What I mean by this 我的意思是

============================== std::vector
^ Processing by a single thread 

==========  ==========  ==========  
^ thread 1  ^ thread 2   ^ thread 3

So ideally I would have thread 1 going from the 1 to 10,000 element thread 2 through the next chunk of elements. 因此,理想情况下,我将使线程1从1到10,000个元素线程2穿过下一个元素块。

Also at the end I want the output to be present in a single vector. 最后,我希望输出以单个矢量形式出现。 So I wont have to create multiple std::vectors and join it. 因此,我不必创建多个std :: vector并将其加入。

If it is of help I am working on creating something like a neural network. 如果有帮助,我正在努力创建类似神经网络的东西。 Though not quite like it, so I can't use the popular implementations of it. 尽管不太喜欢它,所以我不能使用它的流行实现。

What I tried out : (FROM THE SUGGESTION BELOW) 我尝试了什么:(从下面的建议中)

class obj_thread {
private:
    std::mutex m_mutex; 
    std::vector<int> *_data ; 
public: 
    obj_thread(int _size = 0 )
    {
        _data = new std::vector<int>(0); 
        for(int elem_ = 0 ; elem_ < _size ; ++elem_)
            _data->push_back(elem_ * 9);
    }
    ~obj_thread()
    {
        delete _data; 
    }   
    void setElemAt(int _val , int _elem) 
    {
        std::lock_guard<std::mutex> locker(m_mutex);
            _data->at(_elem) = _val;
    };
    int getElem(int _elem) const { return _data->at(_elem);}
    int getSize() const 
    {
    //  std::lock_guard<std::mutex> locker(m_mutex);
        return _data->size();
    };
};

void zeroOut(std::vector<obj_thread*> * _obj , int _beg , int _end)
{
    for(int idx_ = _beg ; idx_ < _end ; ++idx_)
    {
        for(int idxx_ = 0 ; idxx_ < _obj->at(idx_)->getSize() ; ++idxx_)    
            _obj->at(idx_)->setElemAt(0,idxx_);
    }   
}



int main() {

std::vector<obj_thread*> * vec = new std::vector<obj_thread*>(0);
for(unsigned int index_ = 0 ; index_ < _SIZE_ ; ++index_)   
    vec->push_back(new obj_thread(_SIZE_));


    std::thread thread1(zeroOut,vec,_SIZE_/4,_SIZE_/2);
    std::thread thread2(zeroOut,vec,_SIZE_/2,_SIZE_*3/4);
    std::thread thread3(zeroOut,vec,_SIZE_*3/4,_SIZE_);

    thread1.join();
    thread2.join();
    thread3.join();

return 0 ; 
}

Model your operation after std::copy. 在std :: copy之后为您的操作建模。

Something along the line 沿线的东西

#include <thread>

std::vector<int> in; // make whatever size you want
std::vector<int> out;

auto m_in  = in.cbegin() + in.size()/2;
auto m_out = out.begin() + in.size()/2;

std::thread t1(std::copy, in.cbegin(), m_in, out.begin());
std::thread t2(std::copy, m_in, in.cend(), m_out);

t1.join();
t2.join();

This will supposedly copy half of the incoming array in one thread, and second half in another thread. 据推测,这将在一个线程中复制传入数组的一半,而在另一个线程中复制后一半。 Not tested! 未经测试!

If this is what you want, now you have to write function similar to std::copy, just replace assignment with your domain specific processing 如果这是您想要的,现在您必须编写类似于std :: copy的函数,只需将赋值替换为您的域特定处理

http://www.cplusplus.com/reference/algorithm/copy/ http://www.cplusplus.com/reference/algorithm/copy/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM