What is the best way to share data containers between threads in c++

  • A+
Category:Languages

I have an application which has a couple of processing levels like:

InputStream->Pre-Processing->Computation->OutputStream 

Each of these entities run in separate thread. So in my code I have the general thread, which owns the

std::vector<ImageRead> m_readImages; 

and then it passes this member variable to each thread:

InputStream input{&m_readImages}; std::thread threadStream{&InputStream::start, &InputStream}; PreProcess pre{&m_readImages}; std::thread preStream{&PreProcess::start, &PreProcess}; ... 

And each of these classes owns a pointer member to this data:

std::vector<ImageRead>* m_ptrReadImages; 

I also have a global mutex defined, which I lock and unlock on each read/write operation to that shared container. What bothers me is that this mechanism is pretty obscure and sometimes I get confused whether the data is used by another thread or not.

So what is the more straightforward way to share this container between those threads?


The process you described as "Input-->preprocessing-->computation-->Output" is sequential by design: each step depends on the previous one so parallelization in this particular manner is not beneficial as each thread just has to wait for another to complete. Try to find out which step takes most time and parallelize that. Or try to set up multiple parallel processing pipelines that operate sequentially on independent, individual data sets. A usual approach for that would employ a processing queue which distributes the tasks among a set of threads.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: