11.5.8

mail@pastecode.io avatar
unknown
csharp
a year ago
689 B
0
Indexable
Never
using System.Threading;

public class CustomBarrier
{
    private readonly object _lock = new object();
    private readonly int _participantCount;
    private int _currentCount;

    public CustomBarrier(int participantCount)
    {
        _participantCount = participantCount;
        _currentCount = 0;
    }

    public void Wait()
    {
        lock (_lock)
        {
            _currentCount++;
            if (_currentCount == _participantCount)
            {
                _currentCount = 0;
                Monitor.PulseAll(_lock);
            }
            else
            {
                Monitor.Wait(_lock);
            }
        }
    }
}