The following program counts to hundret from two threads, both using the same iterator variable i.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
namespace Threads
{
class Program
{
static int i = 0;
[DllImport("Kernel32.dll"), SuppressUnmanagedCodeSecurity]
public static extern int GetCurrentProcessorNumber();
static void Main(string[] args)
{
object lockObject = new object();
var t1 = new Thread(CountToHundret);
var t2 = new Thread(CountToHundret);
t1.Start(lockObject);
t2.Start(lockObject);
Console.ReadKey();
}
private static void CountToHundret(object lockObject)
{
while (i < 100)
{
lock (lockObject)
{
Console.WriteLine(
++i + "/" +
Thread.CurrentThread.ManagedThreadId + "/" +
GetCurrentProcessorNumber());
}
}
}
}
}
After each count, the counting thread writes i, its own ID and the ID of the core it is running on to the console.
I got the information how to access the core ID from another question:
How to get the core/processor id of executing thread?
This is the output I receive:
1/3/7
2/4/10
[...]
44/4/10
45/3/7
[...]
60/3/6
61/4/0
[...]
100/4/9
Why are there only few thread ID changes in the second column?
Column three shows that this code is executed by multiple cores. So there should be parallelism, and I would expect the following to happen: thread 4 (running on a different core than thread 3) has to wait for the LockObject while thread 3 does its output to the console. Then thread 4 receives the LockObject. Now thread 4 writes to the console. In the meantime, thread 3 reaches the point where it must acquire the LockObject again. It waits until thread 4 has written its output, and then it is again thread 3's turn to acquire the LockObject, and so on.
In short: I'd expect to see thread ID changes in each single line. But why is this not the case here?