I have a collection of clients on server's side. And there are some objects that need to work with that collection - adding and removing clients, sending message to them, updating connection settings and so on. They should perform these actions simultaneously, so mutex or another synchronization primitive is required. I want to share one instance of collection between these objects, but all of them require access to private fields of collection. I hope that code sample makes it more clear[C++]:
class Collection
{
std::vector< Client* > clients;
Mutex mLock;
...
}
class ClientNotifier
{
void sendMessage()
{
mLock.lock();
// loop over clients and send message to each of them
}
}
class ConnectionSettingsUpdater
{
void changeSettings( const std::string& name )
{
mLock.lock();
// if client with this name is inside collection, change its settings
}
}
As you can see, all these classes require direct access to Collection's private fields.
Can you give me an advice about how to implement such behaviour correctly, i.e. keeping Collection's interface simple without it knowing about its users?