Re: Mutex misunderstanding



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;
}
}

.



Relevant Pages

  • Re: Thread Communication in C#
    ... If I use ManualResetEvent, I still need some way to pass data between the ... sure how to pass the reference to the queue to the sub thread at start. ... >> GetMessage. ... >> Pipes and GetMessage both did. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Threading in .Net...
    ... the way it provides messaging between threads is a little ... monitor like the critical section variables to protect the global ... item is placed on the queue, ... When it's done it can reset the ManualResetEvent and wait until ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to improve performance of Queue accessing between 2 threads?
    ... fast as possible once Queue is not empty. ... AutoReset- or ManualResetEvent? ... Thats slow. ...
    (microsoft.public.dotnet.framework)
  • Re: Seeing lock order reversal
    ... 21 vm page queue free mutex -- ... 18 UMA zone -- ... 18 sleep mtxpool -- ... 15 process lock -- ...
    (freebsd-current)
  • Re: location of bioq lock
    ... queue is owned by the driver, and the locking scheme remains the same. ... from a different scheduler than the default, it can be easily plugged in. ... process you have to lock each queue before playing with it. ...
    (freebsd-current)

Loading