# Using Barrier to Implement Go's Wait Group

I have implemented a WaitGroup class to simulate WaitGroup in Go lang. Then I've found that I can use Barrier in a different manner, to achieve the same thing. My WaitGroup has some additional functionality, but in most scenarios Barrier just would do.

Question: Do you spot any design flaws in this style of using Barrier?

Code:

I have an instance of Barrier:

static Barrier WaitGroupBarrier = new Barrier(0);


for (int i = 0; i < 1000; i++) // in multiple places
{
var t = new Task(() =>
{
try
{

// body
}
finally
{
WaitGroupBarrier.RemoveParticipant();
}
});

t.Start();
}


And I wait for them to complete, this way:

if (WaitGroupBarrier.ParticipantsRemaining > 0)
{
Console.WriteLine("waiting...");
WaitGroupBarrier.SignalAndWait();
}


## migrated from stackoverflow.comJun 11 '13 at 16:30

This question came from our site for professional and enthusiast programmers.

• The question is about C#; not Go. I am "simulating" this feature of Go, in C#. It's not a comparison. BTW I like Go. – Kaveh Shahbazian Jun 11 '13 at 9:30
• @svick How can I move this question there? Should I repeat it there or somebody would migrate it? Please guide me. – Kaveh Shahbazian Jun 11 '13 at 14:28
• Probably the fastest way for you is to ask the question there again and then delete it here. Another option would be to flag it and ask for it to be moved. – svick Jun 11 '13 at 15:38
• OP, if you do this, let a link here in comment so that we can follow the question. – Denys Séguret Jun 11 '13 at 15:52
• Yes, I'd like to see the answer to this... My C# skills are a few (read many) years old, and I'd like to see the solution! – Intermernet Jun 12 '13 at 10:49

if (WaitGroupBarrier.ParticipantsRemaining > 0) {