Search code examples
c#multithreadingbarrier

Register new thread to already defined barrier


Is there a way to add a thread to already pre-defined barrier?

The scenario: I have at certain point of time N threads, and the code declares the Barrier in order to handle them.

The problem is, that sometimes I may need another new thread to be handled inside that barrier instance, but the barrier has already been declared with N threads only.

Example:

barrier = new Barrier(N, (sprint) => { 
       Console.WriteLine($"Current sprint: {sprint.CurrentPhaseNumber}")
});

After the declaration I need to update it again somehow with N+1 threads, any suggestions?


Solution

  • The full documentation on Barrier is here: https://learn.microsoft.com/en-us/dotnet/standard/threading/barrier

    In a nutshell, you can add or remove a participant at any time by calling respectively AddParticipant or RemoveParticipant.