I've got a ConcurrentStack that I'm dumping items into. What's a good way to process those items one at a time when the stack isn't empty? I'd like to do this in a way that isn't eating up CPU cycles when the stack isn't being processed.
What I've currently got is basically this and it doesn't seem like an ideal solution.
private void AddToStack(MyObj obj)
{
stack.Push(obj);
HandleStack();
}
private void HandleStack()
{
if (handling)
return;
Task.Run( () =>
{
lock (lockObj)
{
handling = true;
if (stack.Any())
{
//handle whatever is on top of the stack
}
handling = false;
}
}
}
So the bool is there so multiple threads don't get backed up waiting on the lock. But I don't want multiple things handling the stack at once hence the lock. So if two separate threads do end up calling HandleStack simultaneously and get past the bool, the lock is there so both aren't iterating through the stack at once. But once the second gets through the lock the stack'll be empty and doesn't do anything. So this does end up giving me the behavior I want.
So really I'm just writing a pseudo concurrent wrapper around the ConcurrentStack and it seems like there's got to be a different way to achieve this. Thoughts?