Semaphore race condition?
Posted
by poindexter12
on Stack Overflow
See other posts from Stack Overflow
or by poindexter12
Published on 2010-04-21T03:34:22Z
Indexed on
2010/04/21
3:43 UTC
Read the original article
Hit count: 410
I have created a "Manager" class that contains a limited set of resources. The resources are stored in the "Manager" as a Queue. I initialize the Queue and a Semaphore to the same size, using the semaphore to block a thread if there are no resources available. I have multiple threads calling into this class to request a resource. Here is the psuedo code:
public IResource RequestResource()
{
IResource resource = null;
_semaphore.WaitOne();
lock (_syncLock)
{
resource = _resources.Dequeue();
}
return resource;
}
public void ReleaseResource(IResource resource)
{
lock (_syncLock)
{
_resources.Enqueue(resource);
}
_semaphore.Release();
}
While running this application, it seems to run fine for a while. Then, it seems like my Queue is giving out the same object. Does this seem like its possible? I'm pulling my hair out over here, and any help would be greatly appreciated. Feel free to ask for more information if you need it. Thanks!
© Stack Overflow or respective owner