I'm designing a program with a server that let's two clients communicate. There's one client that executes commands and another that makes the other client execute them.
To make this work, I have two threads: one thread for the controlled-client, another for the controller-client.
The controlled-client always stays connected, has a vector with tasks and executes these tasks if a task is added. It has an infinite while loop which is stopped when the connection is closed.
The controller-client adds tasks to the vector.
Now since there is an vector shared between two threads a race condition may occur, but since one thread only adds objects and the other only pops objects, is that necessary? I tried to make a flowchart with this problem, but maybe it isn't clear. I don't really know how to create a flowchart:

I'm using std::vector <CustomClass> from C++ to realize this.
Thanks in advance,
ief2
EDIT: Additional Question: Does the vector.size() call need a mutex?
Yes, this needs a mutex.
vector.pop_front() and vector.push_back() executing at the same time would create all sorts of mess - assuming the nonstandard vector.pop_front() does what the name implies.
Side note: use a queue or a list rather than a vector
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With