Call thread.sleep (), waiting for a new thread to appear

I believe this question can be reduced to “SpinWait vs. Block?”, But I thought there might be a more interesting answer about why almost every C # thread tutorial offers the following call:

Thread newThread = new Thread(new ThreadStart(ThreadMethod));
newThread.Start()
while (!newThread.isAlive()) ;
Thread.Sleep(1); // Allow the new thread to do some work

Unlike locking:

Thread newThread = new Thread(new ThreadStart(ThreadMethod));
newThread.Start()
while (!newThread.isAlive()) Thread.Sleep(1);
Thread.Sleep(1); // Allow the new thread to do some work

My very crude testing (surrounding the while loop with DateTime.Ticks calls) actually shows nothing (says that in both cases the difference is 0 ticks).

Is the process of creating a thread short enough for spinning to be more efficient? Or do most tutorials offer a spin because it is a little more elegant and the time difference is negligible?

+3
source share
3 answers

, - , - , , "Alive" doesn ' t , . , - , , WaitHandle SpinLock.

+2

, - . , .

+1

All Articles