Gate controlling the transit of threads for certain operations.
Class Barrier( [mode] ) from \ Waitable( )
mode | Set to true to initialize the barrier to open. |
The Barrier synchronization structure is a structure that can be either acquired by all or none of the threads willing to acquire it. If the barrier is open, then any wait on it will cause the calling thread to acquire it and proceed immediately, while if it's closed the waiting threads will be blocked forever, until the barrier gets open from the outside.
The methods open and close control the behavior of the barrier.
One use for the barriers is that of communicating a pool of threads a kind termination request; by sharing the barrier with all the threads in the pool, a controlling thread may control their behavior; if they wait on the barrier and on other resources, when the barrier is open they will acquire it, and in this way they will know that is time for a clean termination:
class AThread( struct, bar ) from Thread bar = bar struct = struct ... function run() loop acquired = self.wait( self.bar, self.struct ) if acquired == self.bar // end... return nil end //... work on self.struct end end end
Release is a no-op for a barrier.
Note: By default, the barrier is created in closed status. To create it in open status, pass the mode parameter as a true value.
Methods | |
close | Closes the barrier. |
open | Opens the barrier. |
Methods inherited from class Waitable | |
release | Releases a structure acquired by a waiting function. |
Closes the barrier.
Barrier.close()
Prevents any thread to acquire the barrier. From this moment on, all the threads trying to wait on this barrier will block.
Opens the barrier.
Barrier.open()
Allow all the waiting threads to pass through the barrier.