4

Using python syntax,

threading.Barrier(NUM_THREADS) establishes a control point and all participating threads block until all of the participating “parties” have reached that point. It lets threads start up separately and then pause until they are all ready to proceed.

Can someone explain, a real time scenario(multi-thread), on usage of barrier?

overexchange
  • 2,245
  • 2
  • 17
  • 47
  • 2
    **You've already stated the criteria in your question:** *when you need a control point where all participating threads block until all of the participating “parties” have reached that point; a control point that lets threads start up separately and then pause until they are all ready to proceed.* – Robert Harvey Jul 22 '17 at 18:22
  • @RobertHarvey Example would help me, visualize this context – overexchange Jul 22 '17 at 18:24
  • 1
    I found [many such examples in a Google search.](https://www.google.com/search?q=python+threading+barrier+example) Have you checked out those examples yet? – Robert Harvey Jul 22 '17 at 18:26
  • This question would work better if rephrased in the same vein as _[What is a thread pool?](https://softwareengineering.stackexchange.com/questions/173575/what-is-a-thread-pool)_ – Blrfl Jul 22 '17 at 18:29
  • @RobertHarvey I read that [example](https://docs.python.org/3/library/threading.html#barrier-objects), which is incorrect in usage. – overexchange Jul 22 '17 at 18:30
  • 1
    What do you mean by "incorrect?" That example comes directly from the Python Software Foundation, so I very much doubt that it is incorrect. – Robert Harvey Jul 22 '17 at 18:33
  • Am not sure, How a client and server run in same process and talk to each other? Even if they run as different process, there is no such practice to wait on barrier and then connect to server, once available Server not available code logic goes under Error handling. – overexchange Jul 22 '17 at 18:37

2 Answers2

4

Barriers are used to make multiple threads within the same program do something in a synchronized fashion.

The real-world analog would be the starting gate at a horse race. Ten horses in a race won't arrive at the gate at the same time, but it isn't until all ten of them have entered the gate and are ready to race does the gate open and each horse starts its race. (Starting gates are also called starting barriers because they were barriers rather than gates, which might be where the threading construct got its name.)

Threading's barrier does the same thing. In a system where each thread has a function and all must be ready before any starts operating, the way to enforce that is have the threads initialize and then meet at a barrier. You might see this in the system that runs the engine in your car, where the thread overseeing ignition wouldn't want to operate until it can be sure the threads that handle the throttle and crank position sensors are ready to provide useful data.

Blrfl
  • 20,235
  • 2
  • 49
  • 75
2

An example usage is something like a worker pool where you would want all of your workers to report ready before sending them work. Or something like the subsystems of a game loading up in parallel. Or the map steps in map-reduce...

It's just a tool.

Telastyn
  • 108,850
  • 29
  • 239
  • 365