2

I am going to create thread like this :

static void Main(string[] args)
{            

    Thread tr2 = new Thread(() =>
    {
        int a = 0;

        Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
    });


    tr2.Start();

    Console.ReadKey();
}

But the tr2 won't start , it will start after the ReadKey() method ,and when I add this first line to Main method , tr2 starts before ReadKey() method:

static void Main(string[] args)
{
    Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);



    Thread tr2 = new Thread(() =>
    {
        int a = 0;

        Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
    });



    tr2.Start();

    Console.ReadKey();
}

Where is the fault ?

ebb
  • 9,297
  • 18
  • 72
  • 123
S.A.Parkhid
  • 2,772
  • 6
  • 28
  • 58

3 Answers3

2

http://msdn.microsoft.com/en-us/library/471w8d85.aspx

The ReadKey method waits, that is, blocks on the thread issuing the ReadKey method, until a character or function key is pressed. A character or function key can be pressed in combination with one or more Alt, Ctrl, or Shift modifier keys. However, pressing a modifier key by itself will not cause the ReadKey method to return.

I think ReadKey block the execution of the Console, try to add a breakpoint at this line:

int a = 0;

The thread should start and it will breaks.

The problem is: the main thread is in the execution of Console.ReadKey that is waiting for an event and the second thread wants to write a line in the same console. It's incompatible and the result is dependant of the computer.

ebb
  • 9,297
  • 18
  • 72
  • 123
Nic007
  • 634
  • 5
  • 13
  • seems correct. so when i add WriteLine() , there is a Single Instruction which makes CPU to not to run the read key ... – S.A.Parkhid Jun 04 '13 at 20:24
2

Console.ReadKey() acquires the lock on an internal object in the Console class, called Console.InternalSyncObject.

In short it means that Console.WriteLine will be waiting for the lock to be released, before executing - and as you may have guessed, the lock will be released whenever you press a key.

ebb
  • 9,297
  • 18
  • 72
  • 123
  • i think : when i add WriteLine() , there is a Single Instruction which makes CPU to not to run the read key and winner is tr2. – S.A.Parkhid Jun 04 '13 at 20:25
  • @trydis, touché .. Steven gives a much better explanation than me :) – ebb Jun 04 '13 at 20:25
  • @S.A.Parkhid, Nah. The reason for `WriteLine` not executing is due to a race condition, which @Rakkun already have mentioned. However, spinning up a new thread, and executing the given code _may_ take longer time, and therefore your main thread hits `Console.ReadKey()`, before your worker thread hits `Console.WriteLine()`. – ebb Jun 04 '13 at 20:27
1

Where is the fault ?

There is nothing wrong. This behavior is just by accident. If you run your program on another computer with different number of CPU cores, the result might be different. If you expect some special behavior on every computer, you have to synchronize the threads

VladL
  • 12,769
  • 10
  • 63
  • 83
  • don't tell me that the tr2 starts before the main thread ... maybe you mean that the void Main is not Main thread.. yeah? – S.A.Parkhid Jun 04 '13 at 20:21
  • of course the tr2 do not start before the main because main starts the tr2, but it starts it asynchronously so both threads running quasiparallel on one core/one cpu machine or really parallel on other – VladL Jun 04 '13 at 20:24