Skip to content
Advertisement

What is C++ version of Java’s cyclic barrier?

In java, multiple threads can wait all others at a certain point so that they don’t start a new block of codes before all others finish first block:

JavaScript

is there an exact or easy convertion to C++?

Also with OpenCL, there is a similar instruction:

JavaScript

so all neighbor threads wait each other but it is only a constrained C implementation.

Advertisement

Answer

C++ STL doesn’t have a Cyclic Barrier. You may propose one to the standards committee 🙂

A company like Oracle or Microsoft can quickly decide what to add to their language’s library. For C++, people have to come to an agreement, and it can take a while.

256 threads is a lot. As with all performance-related questions, you need to measure the code to make an informed decision. With 256 threads I would be tempted to use 10 barriers that are synchronized by an 11th barrier. You need to measure to know if that’s actually better.

Check out my C++ implementation of a cyclic barrier, inspired by Java. I wrote it a couple years ago. It’s based it off of someone else’s (buggy) code I found at http://studenti.ing.unipi.it/~s470694/a-cyclic-thread-barrier/ (link doesn’t work anymore…) The code is really simple (no need to credit me). Of course, it’s as is, no warranties.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement