J
Jon Shemitz
The (beta 2) dox say "The time-out ... moves the thread to the ready
queue, bypassing other threads ahead of it in the wait queue, so that
it can reacquire the lock sooner."
To me, this directly states that when I Pulse a locked object that has
threads waiting, a thread that is waiting with a timeout will be moved
to the ready queue ahead of a thread that is waiting without a
timeout. It also rather strongly implies that a thread that is about
to timeout will get priority ahead of a thread that is willing to wait
longer.
Yet, the attached program wakes waiting threads in creation order,
exactly as if a Pulse or PulseAll simply moved threads from the
waiting queue to the ready queue in the order that they entered the
waiting queue. (Run the console app from a DOS box ...)
Are the dox wrong, or am I misinterpreting my test program?
--
www.midnightbeach.com
using System;
using System.Threading;
namespace WaitPriorities
{
class Program
{
static void Main(string[] args)
{
object Lock = new object();
new Thread(new ParameterizedThreadStart(NoTimeout)).Start(Lock);
new Thread(new ParameterizedThreadStart(LongTimeout)).Start(Lock);
new Thread(new ParameterizedThreadStart(ShortTimeout)).Start(Lock);
//lock (Lock)
// Monitor.PulseAll(Lock);
lock (Lock)
Monitor.Pulse(Lock);
lock (Lock)
Monitor.Pulse(Lock);
lock (Lock)
Monitor.Pulse(Lock);
}
static void NoTimeout(object Lock)
{
lock (Lock)
{
Monitor.Wait(Lock);
Console.WriteLine("NoTimeout");
}
}
static void ShortTimeout(object Lock)
{
lock (Lock)
{
bool Locked = Monitor.Wait(Lock, 1000);
Console.WriteLine("ShortTimeout ({0})", Locked ? "Locked" : "Not locked");
}
}
static void LongTimeout(object Lock)
{
lock (Lock)
{
bool Locked = Monitor.Wait(Lock, 1500);
Console.WriteLine("LongTimeout ({0})", Locked ? "Locked" : "Not locked");
}
}
}
}
queue, bypassing other threads ahead of it in the wait queue, so that
it can reacquire the lock sooner."
To me, this directly states that when I Pulse a locked object that has
threads waiting, a thread that is waiting with a timeout will be moved
to the ready queue ahead of a thread that is waiting without a
timeout. It also rather strongly implies that a thread that is about
to timeout will get priority ahead of a thread that is willing to wait
longer.
Yet, the attached program wakes waiting threads in creation order,
exactly as if a Pulse or PulseAll simply moved threads from the
waiting queue to the ready queue in the order that they entered the
waiting queue. (Run the console app from a DOS box ...)
Are the dox wrong, or am I misinterpreting my test program?
--
www.midnightbeach.com
using System;
using System.Threading;
namespace WaitPriorities
{
class Program
{
static void Main(string[] args)
{
object Lock = new object();
new Thread(new ParameterizedThreadStart(NoTimeout)).Start(Lock);
new Thread(new ParameterizedThreadStart(LongTimeout)).Start(Lock);
new Thread(new ParameterizedThreadStart(ShortTimeout)).Start(Lock);
//lock (Lock)
// Monitor.PulseAll(Lock);
lock (Lock)
Monitor.Pulse(Lock);
lock (Lock)
Monitor.Pulse(Lock);
lock (Lock)
Monitor.Pulse(Lock);
}
static void NoTimeout(object Lock)
{
lock (Lock)
{
Monitor.Wait(Lock);
Console.WriteLine("NoTimeout");
}
}
static void ShortTimeout(object Lock)
{
lock (Lock)
{
bool Locked = Monitor.Wait(Lock, 1000);
Console.WriteLine("ShortTimeout ({0})", Locked ? "Locked" : "Not locked");
}
}
static void LongTimeout(object Lock)
{
lock (Lock)
{
bool Locked = Monitor.Wait(Lock, 1500);
Console.WriteLine("LongTimeout ({0})", Locked ? "Locked" : "Not locked");
}
}
}
}