So I know you can use a semaphore and possibly other methods to determine if a lock will succeed and/or how many threads are waiting for the lock but can you do this from within the lock itself?
My situation is I have an event handler that could be called by many objects within a List. I want to do a test within a lock to see if all the objects are in the right state before proceeding. The objects transition to the appropriate state and then raise this event so in a single thread model when the last one raises this event, all objects will be in the right state and we proceed. But it occurred to me that, with multiple threads, multiple objects may be transitioned to the state I’m checking for but as yet have not processed this event as they are waiting at the lock. Therefore the conditional state check within the lock will be true but proceeding would be bad till all threads have finished processing this event. I need it to only be true as the last thread is processed to ensure we don’t proceed too soon.
For example in semi realistic code:
object _LockObj = new object();
void Event_Handler(object sender, EventArgs e)
{
MyObject originator = sender as MyObject;
if(originator == null)
return;
*Do stuff with the originator*
lock(_LockObj)
{
if(ListOfMyObjects.FindAll(o => o.State == DesiredState)
.Count == ListOfMyObjects.Count
&& *nothing waiting at the lock*)
{
*Proceed*
}
}
}
I’m perfectly prepared to accept my approach is smelly to start with and that the solution I’m looking for is moot if I do it properly in the first place but I’m uncertain how to do it properly in a thread safe manner.
I was going to add yet more states to MyObject and manage the flow by setting the appropriate State in the *Do stuff with the originator* section but it didn’t feel correct to be transitioning MyObject from here, not to mention I then have to implement a holding state for each state that raises an event which is getting smellier not simpler!
So if there is a simple way of doing the ‘nothing waiting at the lock‘ check then that’s the way I’d like to go unless I’m missing a really simple to implement pattern that helps in this scenario.
First off, unfortunately, testing for other threads waiting on the lock() statement will not get you what you want. For example, if objects A, B and C have transitioned to the desired state and have raised their events, it’s possible that the
Event_Handleris only called for one object, say A at that point. The same handler will run for B and C at some point, later. So all your objects are now supposedly in the desired state, AND no threads are waiting at the lock, but you prob. don’t want to “proceed”. Even if you do, you need to consider, the same might happen an instant later for B and then again for C, so “proceed” may run three times…It’s a simple suggestion, but you can use a counter to test how many objects have actually raised the event. You could also use another list if a counter doesn’t give sufficient information. The important part is that whichever method you use, only update the counter / list / other mechanism while inside a lock on the same
_LockObj. Then probably reset that counter after proceed has run. If you need to access the counter somewhere else, make sure you use the samelock(_LockObj), this will ensure you’re not changing it while inside the other critical section.Edit: Removed redundant check for objects’ state.