N
not_a_commie
Here's some code I wrote trying to track down a deadlock bug. I was
unable to make this code deadlock on my Core 2 Duo. I'd be interested,
though, if y'all think it is the right way to use thread affinity or
not. I'd also be interested if you saw any code that might cause a
deadlock in some other scenario.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace TestThreading
{
public sealed class Program
{
static private Thread _backgroundThread = null;
static private EventWaitHandle _tickCountUpdateRequest = new
EventWaitHandle(false, EventResetMode.AutoReset);
static private EventWaitHandle _tickCountUpdateDone = new
EventWaitHandle(false, EventResetMode.AutoReset);
static public long CurrentTickCount = 0;
private static void _BackgroundThreadFunc()
{
Thread.BeginThreadAffinity(); // always read the tick count from
the same CPU -- not necessary in Vista
while (true)
{
_tickCountUpdateRequest.WaitOne();
CurrentTickCount = System.Diagnostics.Stopwatch.GetTimestamp();
_tickCountUpdateDone.Set();
}
Thread.EndThreadAffinity(); // never called; but here for symmetry
}
static Program() {
_backgroundThread = new Thread(new
ThreadStart(_BackgroundThreadFunc));
_backgroundThread.IsBackground = true;
_backgroundThread.Start();
}
public long GetCount()
{
lock (_backgroundThread)
{
Program._tickCountUpdateRequest.Set();
Program._tickCountUpdateDone.WaitOne();
return CurrentTickCount;
}
}
private static void _GetCountFunc() {
Random r = new Random();
Program p = new Program();
while (true)
{
// sleep a random amount of time then read
Thread.Sleep(r.Next(20));
lock (System.Console.Out)
{
System.Console.Out.WriteLine("thread " +
Thread.CurrentThread.ManagedThreadId +
": " + p.GetCount().ToString());
}
}
}
static void Main(string[] args)
{
Random r = new Random();
Thread d = new Thread(new ThreadStart(_GetCountFunc));
d.Start();
while (true)
{
Thread a = new Thread(new ThreadStart(_GetCountFunc));
Thread b = new Thread(new ThreadStart(_GetCountFunc));
Thread c = new Thread(new ThreadStart(_GetCountFunc));
a.Start();
b.Start();
c.Start();
Thread.Sleep(r.Next(50));
a.Abort();
Thread.Sleep(r.Next(50));
b.Abort();
c.Abort();
}
}
}
}
unable to make this code deadlock on my Core 2 Duo. I'd be interested,
though, if y'all think it is the right way to use thread affinity or
not. I'd also be interested if you saw any code that might cause a
deadlock in some other scenario.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace TestThreading
{
public sealed class Program
{
static private Thread _backgroundThread = null;
static private EventWaitHandle _tickCountUpdateRequest = new
EventWaitHandle(false, EventResetMode.AutoReset);
static private EventWaitHandle _tickCountUpdateDone = new
EventWaitHandle(false, EventResetMode.AutoReset);
static public long CurrentTickCount = 0;
private static void _BackgroundThreadFunc()
{
Thread.BeginThreadAffinity(); // always read the tick count from
the same CPU -- not necessary in Vista
while (true)
{
_tickCountUpdateRequest.WaitOne();
CurrentTickCount = System.Diagnostics.Stopwatch.GetTimestamp();
_tickCountUpdateDone.Set();
}
Thread.EndThreadAffinity(); // never called; but here for symmetry
}
static Program() {
_backgroundThread = new Thread(new
ThreadStart(_BackgroundThreadFunc));
_backgroundThread.IsBackground = true;
_backgroundThread.Start();
}
public long GetCount()
{
lock (_backgroundThread)
{
Program._tickCountUpdateRequest.Set();
Program._tickCountUpdateDone.WaitOne();
return CurrentTickCount;
}
}
private static void _GetCountFunc() {
Random r = new Random();
Program p = new Program();
while (true)
{
// sleep a random amount of time then read
Thread.Sleep(r.Next(20));
lock (System.Console.Out)
{
System.Console.Out.WriteLine("thread " +
Thread.CurrentThread.ManagedThreadId +
": " + p.GetCount().ToString());
}
}
}
static void Main(string[] args)
{
Random r = new Random();
Thread d = new Thread(new ThreadStart(_GetCountFunc));
d.Start();
while (true)
{
Thread a = new Thread(new ThreadStart(_GetCountFunc));
Thread b = new Thread(new ThreadStart(_GetCountFunc));
Thread c = new Thread(new ThreadStart(_GetCountFunc));
a.Start();
b.Start();
c.Start();
Thread.Sleep(r.Next(50));
a.Abort();
Thread.Sleep(r.Next(50));
b.Abort();
c.Abort();
}
}
}
}