Re: Mutex misunderstanding
- From: mccownf@xxxxxxxxx
- Date: 1 Apr 2005 07:46:26 -0800
I found a solution to my problem without using a mutex. I used a
ManualResetEvent instead. Here's my solution in case anyone is
interested:
public class TestRemote : MarshalByRefObject
{
private Queue myQ = new Queue();
private ManualResetEvent manualEvent = new ManualResetEvent(false);
void AddToQ(Task t) {
lock (unassignedTasks)
{
myQ.Enqueue(t);
// Signal others there is something in the queue.
manualEvent.Set();
}
}
Task RemoveFromQ() {
Task t = null;
while (t == null) {
if (myQ.Count == 0) {
// Queue is empty so wait until something is added.
manualEvent.WaitOne();
}
lock (myQ) {
// Must check again because previous check was on unlocked
queue.
if (myQ.Count != 0) {
t = (Task)myQ.Dequeue();
if (myQ.Count == 0) {
// Nothing left in queue so lock it again.
manualEvent.Reset();
}
}
} // end lock
} // end while
return t;
}
}
.
- Follow-Ups:
- Re: Mutex misunderstanding
- From: Mehdi
- Re: Mutex misunderstanding
- References:
- Mutex misunderstanding
- From: mccownf
- Re: Mutex misunderstanding
- From: Mehdi
- Re: Mutex misunderstanding
- From: mccownf
- Mutex misunderstanding
- Prev by Date: The underlying connection was closed: An unexpected error occurred on a receive
- Next by Date: Re: Mutex misunderstanding
- Previous by thread: Re: Mutex misunderstanding
- Next by thread: Re: Mutex misunderstanding
- Index(es):
Relevant Pages
|
Loading