Barrier Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Memungkinkan beberapa tugas bekerja secara kooperatif pada algoritma secara paralel melalui beberapa fase.
public ref class Barrier : IDisposable
public class Barrier : IDisposable
[System.Runtime.InteropServices.ComVisible(false)]
public class Barrier : IDisposable
type Barrier = class
interface IDisposable
[<System.Runtime.InteropServices.ComVisible(false)>]
type Barrier = class
interface IDisposable
Public Class Barrier
Implements IDisposable
- Warisan
-
Barrier
- Atribut
- Penerapan
Contoh
Contoh berikut menunjukkan cara menggunakan hambatan:
using System;
using System.Threading;
using System.Threading.Tasks;
class BarrierDemo
{
// Demonstrates:
// Barrier constructor with post-phase action
// Barrier.AddParticipants()
// Barrier.RemoveParticipant()
// Barrier.SignalAndWait(), incl. a BarrierPostPhaseException being thrown
static void BarrierSample()
{
int count = 0;
// Create a barrier with three participants
// Provide a post-phase action that will print out certain information
// And the third time through, it will throw an exception
Barrier barrier = new Barrier(3, (b) =>
{
Console.WriteLine("Post-Phase action: count={0}, phase={1}", count, b.CurrentPhaseNumber);
if (b.CurrentPhaseNumber == 2) throw new Exception("D'oh!");
});
// Nope -- changed my mind. Let's make it five participants.
barrier.AddParticipants(2);
// Nope -- let's settle on four participants.
barrier.RemoveParticipant();
// This is the logic run by all participants
Action action = () =>
{
Interlocked.Increment(ref count);
barrier.SignalAndWait(); // during the post-phase action, count should be 4 and phase should be 0
Interlocked.Increment(ref count);
barrier.SignalAndWait(); // during the post-phase action, count should be 8 and phase should be 1
// The third time, SignalAndWait() will throw an exception and all participants will see it
Interlocked.Increment(ref count);
try
{
barrier.SignalAndWait();
}
catch (BarrierPostPhaseException bppe)
{
Console.WriteLine("Caught BarrierPostPhaseException: {0}", bppe.Message);
}
// The fourth time should be hunky-dory
Interlocked.Increment(ref count);
barrier.SignalAndWait(); // during the post-phase action, count should be 16 and phase should be 3
};
// Now launch 4 parallel actions to serve as 4 participants
Parallel.Invoke(action, action, action, action);
// This (5 participants) would cause an exception:
// Parallel.Invoke(action, action, action, action, action);
// "System.InvalidOperationException: The number of threads using the barrier
// exceeded the total number of registered participants."
// It's good form to Dispose() a barrier when you're done with it.
barrier.Dispose();
}
}
Imports System.Threading
Imports System.Threading.Tasks
Module BarrierSample
' Demonstrates:
' Barrier constructor with post-phase action
' Barrier.AddParticipants()
' Barrier.RemoveParticipant()
' Barrier.SignalAndWait(), incl. a BarrierPostPhaseException being thrown
Sub Main()
Dim count As Integer = 0
' Create a barrier with three participants
' Provide a post-phase action that will print out certain information
' And the third time through, it will throw an exception
Dim barrier As New Barrier(3,
Sub(b)
Console.WriteLine("Post-Phase action: count={0}, phase={1}", count, b.CurrentPhaseNumber)
If b.CurrentPhaseNumber = 2 Then
Throw New Exception("D'oh!")
End If
End Sub)
' Nope -- changed my mind. Let's make it five participants.
barrier.AddParticipants(2)
' Nope -- let's settle on four participants.
barrier.RemoveParticipant()
' This is the logic run by all participants
Dim action As Action =
Sub()
Interlocked.Increment(count)
barrier.SignalAndWait()
' during the post-phase action, count should be 4 and phase should be 0
Interlocked.Increment(count)
barrier.SignalAndWait()
' during the post-phase action, count should be 8 and phase should be 1
' The third time, SignalAndWait() will throw an exception and all participants will see it
Interlocked.Increment(count)
Try
barrier.SignalAndWait()
Catch bppe As BarrierPostPhaseException
Console.WriteLine("Caught BarrierPostPhaseException: {0}", bppe.Message)
End Try
' The fourth time should be hunky-dory
Interlocked.Increment(count)
' during the post-phase action, count should be 16 and phase should be 3
barrier.SignalAndWait()
End Sub
' Now launch 4 parallel actions to serve as 4 participants
Parallel.Invoke(action, action, action, action)
' This (5 participants) would cause an exception:
' Parallel.Invoke(action, action, action, action, action)
' "System.InvalidOperationException: The number of threads using the barrier
' exceeded the total number of registered participants."
' It's good form to Dispose() a barrier when you're done with it.
barrier.Dispose()
End Sub
End Module
Keterangan
Sekelompok tugas bekerja sama dengan bergerak melalui serangkaian fase, di mana masing-masing dalam grup memberi sinyal telah tiba pada Barrier fase tertentu dan secara implisit menunggu semua orang lain tiba. Hal yang sama Barrier dapat digunakan untuk beberapa fase.
Konstruktor
| Nama | Deskripsi |
|---|---|
| Barrier(Int32, Action<Barrier>) |
Menginisialisasi instans baru dari kelas Barrier. |
| Barrier(Int32) |
Menginisialisasi instans baru dari kelas Barrier. |
Properti
| Nama | Deskripsi |
|---|---|
| CurrentPhaseNumber |
Mendapatkan jumlah fase pembatas saat ini. |
| ParticipantCount |
Mendapatkan jumlah total peserta dalam hambatan. |
| ParticipantsRemaining |
Mendapatkan jumlah peserta dalam hambatan yang belum memberi sinyal dalam fase saat ini. |
Metode
| Nama | Deskripsi |
|---|---|
| AddParticipant() |
Memberi tahu Barrier bahwa akan ada peserta tambahan. |
| AddParticipants(Int32) |
Memberi tahu Barrier bahwa akan ada peserta tambahan. |
| Dispose() |
Merilis semua sumber daya yang digunakan oleh instans Barrier kelas saat ini. |
| Dispose(Boolean) |
Merilis sumber daya yang tidak dikelola yang digunakan oleh Barrier, dan secara opsional merilis sumber daya terkelola. |
| Equals(Object) |
Menentukan apakah objek yang ditentukan sama dengan objek saat ini. (Diperoleh dari Object) |
| GetHashCode() |
Berfungsi sebagai fungsi hash default. (Diperoleh dari Object) |
| GetType() |
Mendapatkan Type instans saat ini. (Diperoleh dari Object) |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| RemoveParticipant() |
Memberi tahu Barrier bahwa akan ada satu peserta yang kurang. |
| RemoveParticipants(Int32) |
Memberi tahu Barrier bahwa akan ada lebih sedikit peserta. |
| SignalAndWait() |
Sinyal bahwa peserta telah mencapai hambatan dan menunggu semua peserta lain untuk mencapai hambatan juga. |
| SignalAndWait(CancellationToken) |
Memberi sinyal bahwa peserta telah mencapai hambatan dan menunggu semua peserta lain mencapai hambatan, sambil mengamati token pembatalan. |
| SignalAndWait(Int32, CancellationToken) |
Sinyal bahwa peserta telah mencapai hambatan dan menunggu semua peserta lain mencapai hambatan juga, menggunakan bilangan bulat bertanda tangan 32-bit untuk mengukur batas waktu, sambil mengamati token pembatalan. |
| SignalAndWait(Int32) |
Sinyal bahwa peserta telah mencapai hambatan dan menunggu semua peserta lain mencapai hambatan juga, menggunakan bilangan bulat bertanda tangan 32-bit untuk mengukur batas waktu. |
| SignalAndWait(TimeSpan, CancellationToken) |
Sinyal bahwa peserta telah mencapai hambatan dan menunggu semua peserta lain mencapai hambatan juga, menggunakan TimeSpan objek untuk mengukur interval waktu, sambil mengamati token pembatalan. |
| SignalAndWait(TimeSpan) |
Sinyal bahwa peserta telah mencapai hambatan dan menunggu semua peserta lain mencapai hambatan juga, menggunakan TimeSpan objek untuk mengukur interval waktu. |
| ToString() |
Mengembalikan string yang mewakili objek saat ini. (Diperoleh dari Object) |
Berlaku untuk
Keamanan Thread
Semua anggota Barrier publik dan terlindungi aman utas dan dapat digunakan secara bersamaan dari beberapa utas, dengan pengecualian Dispose, yang hanya boleh digunakan ketika semua operasi lain pada Barrier telah selesai.
Lihat juga
- Halangan
- Cara: Menyinkronkan Operasi Bersamaan dengan Penghentian