Consider that you have a list:
class CLIENTS
{
public:
CLIENTS();
~CLIENTS();
bool addClient();
bool removeClient();
bool getDataFromClientObj(unsigned int id);
bool storeDataInClientObj(unsigned int id);
private:
// vector, that contains all the clients
boost::ptr_vector<CLIENTOBJ> clients;
// mutex for the client-list
boost::mutex mutex;
};
Consider further, that getDataFromClientObj() acquires a shared lock (mutex, private). In addition to that, you want to be able to get data from a client via getDataFromClient().
If a client has no data at all in his queue, getDataFromClient() shall wait on a condition variable for that client until it has new data to be read.
Well, here's my problem:
As long as getDataFromClient(); waits, (since this is a multiple readers / single writer list), I am not able to add new clients or delete a client, because getDataFromClient() holds the mutex lock.
How would you exactly solve the scenario, if you have a list, that shall be threadsafe + wait on a condition variable for a specific client + while waiting on a client, be able to delete or add any of the clients hold within the list?
So here once again are the facts:
- Threadsafe List (multiple readers / single writer)
- Being able to add a client / delete a client at any time
- Being able to wait on a (specific) condition for each client specifically (one client might have stored new data in his own queue, while another hasn't; then getDataFromClient() shall wait until new data is there to be read)
The problem I think is, that, given that there is one condition for each client (pseudo-code: if(clientsqueue.isEmpty() -> wait) there have to be multiple condition variables (am I wrong?)
Further information:
OS: Windows 7
Language: C++
Compiler VS2010