مشاركة عبر


حاجز (إطار عمل.NET)

وهو تشغيل حاجز الأولية التي تتيح مؤشرات ترابط متعددة (تعرف المشاركين ) للعمل بشكل متزامن تشغيل خوارزمية تشغيل مراحل مزامنة المعرفة من قبل مستخدم. تنفيذ كل مشارك حتى يصل إلى يؤشر حاجز في تعليمات برمجية. حاجز يمثل إنهاء مرحلة واحدة من العمل. When a participant reaches the barrier, it blocks until الجميع participants have reached the same barrier. بعد الجميع participants have reached the barrier, you can optionally invoke a ينشر-مرحلة إجراء. This ينشر-مرحلة إجراء can be used إلى perform الاجراءات بواسطة a مفرد مؤشر ترابط while الجميع غير ذلك عمليات جزئية are still blocked. بعد the إجراء has been executed, the participants are الجميع unblocked.

The following تعليمات برمجية قصاصة shows a أساسى barrier نقش.


' Create the Barrier object, and supply a post-phase delegate 
' to be invoked at the end of each phase.
Dim barrier = New Barrier(2, Sub(bar)
                                 ' Examine results from all threads, determine 
                                 ' whether to continue, create inputs for next phase, etc. 
                                 If (someCondition) Then
                                     success = True
                                 End If
                             End Sub)



' Define the work that each thread will perform. (Threads do not
' have to all execute the same method.)
Sub CrunchNumbers(ByVal partitionNum As Integer)

    ' Up to System.Int64.MaxValue phases are supported. We assume
    ' in this code that the problem will be solved before that.
    While (success = False)

        ' Begin phase:
        ' Process data here on each thread, and optionally
        ' store results, for example:
        results(partitionNum) = ProcessData(myData(partitionNum))

        ' End phase:
        ' After all threads arrive,post-phase delegate
        ' is invoked, then threads are unblocked. Overloads
        ' accept a timeout value and/or CancellationToken.
        barrier.SignalAndWait()
    End While
End Sub

' Perform n tasks to run in in parallel. For simplicity
' all threads execute the same method in this example.
Shared Sub Main()

    Dim app = New BarrierDemo()
    Dim t1 = New Thread(Sub() app.CrunchNumbers(0))
    Dim t2 = New Thread(Sub() app.CrunchNumbers(1))
    t1.Start()
    t2.Start()
End Sub

 // Create the Barrier object, and supply a post-phase delegate 
 // to be invoked at the end of each phase.
 Barrier barrier = new Barrier(2, (bar) => 
     {
         // Examine results from all threads, determine 
         // whether to continue, create inputs for next phase, etc. 
         if (someCondition)
             success = true;
     });       


 // Define the work that each thread will perform. (Threads do not
 // have to all execute the same method.)
 void CrunchNumbers(int partitionNum)
 {
     // Up to System.Int64.MaxValue phases are supported. We assume
     // in this code that the problem will be solved before that.
     while (success == false)
     {
         // Begin phase:
         // Process data here on each thread, and optionally
         // store results, for example:
         results[partitionNum] = ProcessData(data[partitionNum]);

         // End phase:
         // After all threads arrive,post-phase delegate
         // is invoked, then threads are unblocked. Overloads
         // accept a timeout value and/or CancellationToken.
         barrier.SignalAndWait();
     }
 }

 // Perform n tasks to run in in parallel. For simplicity
// all threads execute the same method in this example.
 static void Main()
 {
     var app = new BarrierDemo();
     Thread t1 = new Thread(() => app.CrunchNumbers(0));
     Thread t2 = new Thread(() => app.CrunchNumbers(1));
     t1.Start();
     t2.Start();

 }

لمثال كامل، راجع كيفية القيام بما يلي: تزامن العمليات المتزامنة مع حاجز.

إضافة و Removing Participants

When you إنشاء a Barrier, specify the رقم of participants. You can also إضافة أو إزالة participants dynamically at أي وقت. For مثال, if واحد participant solves its part of the problem, you can store the النتيجة, إيقاف execution تشغيل that مؤشر ترابط, و يتصل RemoveParticipant إلى decrement the رقم of participants في the barrier. When you إضافة a participant بواسطة calling AddParticipant, the return القيمة specifies the الحالي مرحلة رقم, which may be useful في ترتيب إلى يهيّئ the work of the جديد participant.

مقطوع Barriers

Deadlocks can occur if واحد participant fails إلى reach the barrier. إلى avoid these deadlocks, استخدم the التحميلات الزائدة of the SignalAndWait أسلوب إلى specify a الوقت-خارج نقطة و a cancellation token. These التحميلات الزائدة return a منطقي القيمة that every participant can فحص قبل it continues إلى the التالي مرحلة.

ينشر-مرحلة استثناءات

If the ينشر-مرحلة تفويض throws an ‏‏ استثناء, it هو wrapped في a BarrierPostPhaseException كائن which هو then propagated إلى الجميع participants.

Barrier Versus ContinueWhenAll

Barriers are especially useful when the عمليات جزئية are performing multiple phases في loops. If your تعليمات برمجية يتطلب فقط واحد أو الثاني phases of work, consider whether إلى استخدم System.Threading.Tasks.Task الكائنات مع أي نوع of implicit ربط, including:

لمزيد من المعلومات، راجع كيفية: ربط متعدد المهام مع Continuations.

راجع أيضًا:

المهام

كيفية القيام بما يلي: تزامن العمليات المتزامنة مع حاجز

موارد أخرى

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