Задайте вопросЗадайте вопрос
 

ОтвеченоSpinWait - possible bug in implementation

  • 26 июня 2009 г. 3:53Joe AlbahariMVPМедали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     
    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

Ответы

  • 29 июня 2009 г. 20:23Emad OmaraMSFTМедали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     Отвечено

    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

    • Помечено в качестве ответаJoe AlbahariMVP30 июня 2009 г. 13:35
    • Предложено в качестве ответаJosh PhillipsMSFT29 июня 2009 г. 23:00
    •  

Все ответы