-3

I am wondering a bit at the moment. I was just reading a bit about Threads and landed there: Task vs Thread differences [duplicate] here on stackoverflow from Jacek (sorry cant create a link because i can only make 2 with reputation<10)

and the first Comment from MoonKnight led me there: albahari.com/threading

i have taken the code and changed it a little to make it better read able what is happening. Here comes my changed code:

static void Main()
    {
        Thread t = new Thread(WriteY);          // Kick off a new thread
        t.Start();                               // running WriteY()

        // Simultaneously, do something on the main thread.
        for (int i = 0; i < 10; i++) { System.Threading.Thread.Sleep(1); Console.Write(i); };
        Console.ReadLine();
    }

    static void WriteY()
    {
        for (int y = 0; y < 10; y++) { System.Threading.Thread.Sleep(1); Console.Write(y); };
        Console.ReadLine();
    }

what I expected to happen (and what happens most of the time) was this: Good Thread: enter image description here

but here is the thing I am wondering about(its absolutely random and promised the same code):

miracle thread:

enter image description here

my questions:

1.How can this happen that there are different numbers the threads should always run at the same time shouldnt they?

2.all this gets more crazy the lower the sleep time gets so if you remove it completely it fells absolutely random

toha
  • 5,095
  • 4
  • 40
  • 52
  • 4
    *"the threads should always run at the same time?"* -- Nope. Imagine what it would take to guarantee that the two loops in the two threads were synchronized every step of the way. It's possible, but you'd have to write a fair amount of code to make it happen. Many, many processes and threads are running on your computer. They each get little bits of time to do their thing. Threading is messy. The Santa-Claus-level miracle would be if two threads running identical code always happened to do the same things at what *appeared to the user* to be the same time. – 15ee8f99-57ff-4f92-890c-b56153 May 30 '17 at 16:59
  • if you want to sync them, use an appropiate mechanism – Gonzalo.- May 30 '17 at 16:59
  • @ed i see thanks for the fast answer – Pr0gr4mm3r May 30 '17 at 17:14
  • @Gonzalo can you maybe give me an example i am new in coding don´t know the appropiate mechanism but will search for it anyway thank you both – Pr0gr4mm3r May 30 '17 at 17:14

1 Answers1

0

When you execute the first loop on the main thread and start WriteY() on a separate thread, there is absolutely no way to predict the sequence in which events in one thread will happen relative to events in the other thread.

I've written a few tests to demonstrate this. Here's one. And here's another.

What characterizes both of these examples is that very often they will run in the "expected" sequence, but once in a while they won't.

That tells us a few things about multithreaded operations:

  • Concurrencty or parallel execution is beneficial when we want to distribute work across threads, but not when events must occur in a predictable sequence.
  • It requires extra caution because it if we do it wrong it might seem to work anyway. And then once in a while it won't work. Those occasions when it doesn't work will be extremely difficult to debug, one reason being that you won't be able to get the behavior to repeat when you want to.
Scott Hannen
  • 27,588
  • 3
  • 45
  • 62