مشاركة عبر


SpinWait

System.Threading.SpinWait هو a lightweight المزامنة نوع that you can استخدم في منخفض-المستوى scenarios إلى avoid the expensive سياق switches و kernel transitions that are مطلوب for kernel أحداث. تشغيل multicore computers, when a مورد ليس expected إلى be held for long periods of الوقت, it can be المزيد efficient for a جارى الإنتظار مؤشر ترابط إلى spin في مستخدم الوضع for a few dozen أو a few hundred cycles, و then إعادة المحاولة إلى اكتساب the مورد. If the مورد هو متوفر بعد spinning, then you have تم الحفظ several thousand cycles. If the مورد هو still غير متاح, then you have spent فقط a few cycles و can still Enter الزر a kernel-based wait. Th هو تركيبة يختفي بعد ذلك الانتظار هو يشار إليها أحياناً على عملية الانتظار مرحلتين .

SpinWaitهو مصمم للاستخدام بالاقتران مع الأنواع من برنامج.NET Framework التي يلتف kernel الأحداث مثلManualResetEvent. SpinWaitيمكن أن تستخدم بواسطة نفسه للوظائف أساسى يختفي في برنامج واحد فقط.

SpinWaitهو المزيد مجرد عنصر فارغ الحلقة. هو المطبقة بعناية بتوفير سلوك يختفي الصحيح للحالة عام وسوف نفسه بدء تبديل سياق إذا يدور بفترة ممكنة (تقريبا الطول الوقت المطلوب لانتقال kernel). ل مثال، أجهزة الكمبيوتر الأساسية بمفرده، SpinWaitتعطي شريحة الوقت مؤشر ترابط مباشرة لأن توجيه كتل يختفي التقدم تشغيل الجميع عمليات جزئية. SpinWaitتعطي أيضا حتى تشغيل الأجهزة multi-core إلى منع حظر مؤشرات ترابط أولوية أتشغيل أو collecإلىr البيانات المهملة. انتظار مؤشر ترابط ولذلك، إذا كنت تستخدم SpinWaitفي الثانية-مرحلة الانتظار العملية، من المستحسن أن تقوم باستدعاء الانتظار kernel قبل SpinWaitنفسه يبدأ بتبديل سياق. SpinWaitيوفرNextSpinWillYieldخاصية، يمكنك فحص من قبل كل يتصل إلىSpinOnce. عند خاصية إرجاع true، تبدأ عملية انتظار الخاص بك. على سبيل المثال ، راجع كيفية القيام بما يلي: استخدم SpinWait لتنفيذه انتظار الثاني-مرحلة تشغيل.

إذا كنت لا تقوم مرحلتين عملية الانتظار لكن يتم يدور فقط إلى أن بعض شرط هو صحيحاً، يمكنك تمكين SpinWaitلتنفيذ سياقه يبدل ذلك أنه هو citizen بضاعة في بيئة النظام التشغيل Windows. يوضح المثال التالي الأساسية SpinWaitفي قفل قماش قطنية مكدس. إذا احتجت إلى كدسة ذات الأداء العالي، مؤشر ترابط-آمن، خذ بعين الاعتبار استخدام System.Collections.Concurrent.ConcurrentStack<T>.

Imports System.Threading
Module SpinWaitDemo


    Public Class LockFreeStack(Of T)
        Private m_head As Node

        Private Class Node
            Public [Next] As Node
            Public Value As T
        End Class

        Public Sub Push(ByVal item As T)
            Dim spin As New SpinWait()
            Dim head As Node, node As New Node With {.Value = item}

            While True
                Thread.MemoryBarrier()
                head = m_head
                node.Next = head
                If Interlocked.CompareExchange(m_head, node, head) Is head Then Exit While
                spin.SpinOnce()
            End While
        End Sub

        Public Function TryPop(ByRef result As T) As Boolean
            result = CType(Nothing, T)
            Dim spin As New SpinWait()

            Dim head As Node
            While True
                Thread.MemoryBarrier()
                head = m_head
                If head Is Nothing Then Return False
                If Interlocked.CompareExchange(m_head, head.Next, head) Is head Then
                    result = head.Value
                    Return True
                End If
                spin.SpinOnce()
            End While
        End Function
    End Class


End Module
public class LockFreeStack<T>
{
    private volatile Node m_head;

    private class Node { public Node Next; public T Value; }

    public void Push(T item)
    {
        var spin = new SpinWait();
        Node node = new Node { Value = item }, head;
        while (true)
        {
            head = m_head;
            node.Next = head;
            if (Interlocked.CompareExchange(ref m_head, node, head) == head) break;
            spin.SpinOnce();
        }
    }

    public bool TryPop(out T result)
    {
        result = default(T);
        var spin = new SpinWait();

        Node head;
        while (true)
        {
            head = m_head;
            if (head == null) return false;
            if (Interlocked.CompareExchange(ref m_head, head.Next, head) == head)
            {
                result = head.Value;
                return true;
            }
            spin.SpinOnce();
        }
    }
}

راجع أيضًا:

المرجع

SpinWait

موارد أخرى

ترابط الكائنات و الميزات