vendredi 25 mai 2018

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

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?

Aucun commentaire:

Enregistrer un commentaire