title: "Python threading barrier" Why this code works and is there any better way?
body: <p>I have searched for python barrier but there is very few related issues. I am still confused about barrier.wait(), even my code works. </p>
  
  <p>I utilize python barrier to implement such a function: A main thread and n sub-threads. In each round, the main thread waits for all the sub-threads finishing their current work, and then all the threads go to the next round until some condition we meet. Thus, I found that barrier is proper to implement this function, here is my code for main thread.</p>
  
  <pre><code> def superstep(self):
      workers = []
      barrier = threading.Barrier(self.num_workers+1)
      for vertex in self.vertices:
          worker = Worker(vertex, barrier)
          workers.append(worker)
          worker.start()
  
      while self.flag:
          barrier.wait()
          self.distributeMessages()
          self.count += 1
          print ("superstep: ", self.count)
          self.flag = self.isTerminated()
  
      for worker in workers:
          worker.flag = False
  
      for worker in workers:
          worker.join()
  </code></pre>
  
  <ol>
  <li>the first 'for' loop creates n threads named worker and stored in a list workers.</li>
  <li>the 'while' loop is the main thread that waits for other sub-threads, and breaks when self.flag is False.</li>
  <li>the second 'for' loop used for setting flag to False in each worker(sub-threads), telling them to exit loop.</li>
  </ol>
  
  <p>here is my Worker class.</p>
  
  <pre><code>class Worker(threading.Thread):
      def __init__(self, vertex, barrier):
          threading.Thread.__init__(self)
          self.vertex = vertex
          self.flag = True
          self.barrier = barrier
  
      def run(self):
          while self.flag:
              self.barrier.wait()
              do something
  </code></pre>
  
  <p>The code works well, all the threads can join(). But as I looked at <a href="https://docs.python.org/3/library/threading.html#barrier-objects" rel="nofollow noreferrer">python barrier</a>, all the threads will release simultaneously when all the threads call wait(). What if the main threads breaks from its while loop and all the other threads are waiting for it just right, in this case, the second 'for' loop is useless and sub-threads will never join().</p>
  
  <p><strong>So how this code works, is there any other way to exit the barrier instead of raising BrokenBarrierError?</strong> In addition, if I add some code in the second 'for' loop, print some information or something else, the procedure is blocked. I guess there must be some sub-threads that are in wait() and have no chance to check flag, so they cannot exit from run() of threads.</p>