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
In my program the std::vector is large and the operations done on the classes are time consuming . So I was wondering if I could use std::thread to break up the operation on std::vector into chunks . 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.
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.
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.
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