Fazer uma PerguntaFazer uma Pergunta
 

RespondidoSpinWait - possible bug in implementation

  • sexta-feira, 26 de junho de 2009 3:53Joe AlbahariMVPMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    I'd expect the calls to Thread.Sleep(0) and Thread.Yield() to be swapped around:

    public void SpinOnce()
    {
        if (this.NextSpinWillYield)
        {
            CdsSyncEtwBCLProvider.Log.SpinWait_NextSpinWillYield();
            int num = (this.m_count >= 10) ? (this.m_count - 10) : this.m_count;
            if ((num % 20) == 0x13)
            {
                Thread.Sleep(1);
            }
            else if ((num % 5) == 4)
            {
                Thread.Sleep(0);
            }
            else
            {
                Thread.Yield();
            }
        }
        else
        {
            Thread.SpinWait(((int) 4) << this.m_count);
        }
        this.m_count = (this.m_count == 0x7fffffff) ? 10 : (this.m_count + 1);
    }


    Alternatively, it would make sense if it always called Yield instead of Sleep(0).

    Joe

    Write LINQ queries interactively - www.linqpad.net

Respostas

  • segunda-feira, 29 de junho de 2009 20:23Emad OmaraMSFTMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido

    No, it isn't. Yield will break the execution of the thread, place it at the end of the queue for its priority and looking for the highest priority ready thread in the same processor. However Sleep(0) will look for the highest priority ready thread in all processors.

    From the MSDN docs for Thread.Yield:
    "Yielding is limited to the processor that is executing the calling thread. The operating system will not switch execution to another processor, even if that processor is idle or is running a thread of lower priority"

    Emad

Todas as Respostas

  • sexta-feira, 26 de junho de 2009 18:28Emad OmaraMSFTMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Resposta Proposta
    Thanks Joe for the feedback. SpinWait tries different ways of yielding after a certain amount of spinning, it first call Thread.Yield and every X yields it calls Sleep(0) to give its CPU slice to higher priority threads and every Y yields it calls Sleep(1) to sleep the minimum allowed time (usually between 10 and 15 ms)
    where Y >> X we don't wanna move to Sleep(1) quickly.
    Regarding to swapping Sleep(0) with Yield this will make the default yielding using Sleep(0) which would be very expensive to use Sleep that frequent especially for short spinning time.
    Hope that answers your question.

    Emad
  • sábado, 27 de junho de 2009 1:22Joe AlbahariMVPMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    I don't understand. Isn't Sleep(0) supposed to be cheaper than a Yield? 

    According to Joe Duffy , "Sleep(0) actually only gives up the current thread's time-slice if a thread at equal priority is ready to run" while Yield (SwitchToThread) doesn't exhibit this problem.

    If this is not the case, then what's the difference between Sleep(0) and Yield?

    Joe




    Write LINQ queries interactively - www.linqpad.net
  • segunda-feira, 29 de junho de 2009 20:23Emad OmaraMSFTMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido

    No, it isn't. Yield will break the execution of the thread, place it at the end of the queue for its priority and looking for the highest priority ready thread in the same processor. However Sleep(0) will look for the highest priority ready thread in all processors.

    From the MSDN docs for Thread.Yield:
    "Yielding is limited to the processor that is executing the calling thread. The operating system will not switch execution to another processor, even if that processor is idle or is running a thread of lower priority"

    Emad