1

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 ; } 
6
  • "So I was wondering if I could use std::thread to break up the operation on std::vector into chunks" Yes. Commented Nov 22, 2015 at 1:54
  • @JamesRoot Could you point me at some tutorial which achieves this ? Thank you ! Commented Nov 22, 2015 at 1:55
  • Vectors don't store classes.... Commented Nov 22, 2015 at 2:33
  • @LightnessRacesinOrbit Vectors do store instances of a class right ? or pointers to those instances . Are you being pedantic , or is there some important concept about cpp that I missed ? Commented Nov 22, 2015 at 16:17
  • 1
    Why are you newing std::vector? That is usually a mistake. Commented Nov 22, 2015 at 23:55

1 Answer 1

4

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

I tried implementing something similar to what you said. It has been edited into the question. Could you please look at it ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.