I have a list of Dinosaur
objects, that can be added to, removed from, and the dinosaurs themselves need to be fed. This all happens in a highly multi-threaded environment, so the list is mutex protected.
static Mutex s_dinosaurMutex;
static vector<Dinosaur> s_dinosaurList;
void AddDinosaur(const Dinosaur& dinosaur)
{
s_dinosaurMutex.Lock();
s_dinosaurList.push_back(dinosaur);
s_dinosaurMutex.Unlock();
}
void RemoveDinosaur(const Dinosaur& dinosaur)
{
s_dinosaurMutex.Lock();
vector<IMadderReceiver*>::iterator it = find(s_dinosaurList.begin(), s_dinosaurList.end(), dinosaur);
if (it != s_dinosaurList.end())
s_dinosaurList.erase(it);
s_dinosaurMutex.Unlock();
}
void FeedDinosaur(const Dinosaur& dinosaur)
{
s_dinosaurMutex.Lock();
vector<IMadderReceiver*>::iterator it = find(s_dinosaurList.begin(), s_dinosaurList.end(), dinosaur);
if (it != s_dinosaurList.end())
(*it).Feed(); // Feeding a dinosaur can take a long time, ~ 1 second
s_dinosaurMutex.Unlock();
}
Now the problem is in the feeding. This can take a long time, but it's absolutely fine if multiple threads feed the same (or a different) dinosaurs at the same time. The feeding process should therefore not stall other FeedDinosaur
calls, though it should stall for Add and Remove calls, and wait for those to finish. The behaviour at the moment is that many threads are lining up queuing in order to Feed the dinosaurs, which makes the system grind to a hold.
Is there a particular (mutex-like) design patterns that allows this type of behaviour of ok-ing threads that require read-only access?