-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add threading.Barrier #53023
Comments
The "barrier" synchronization primitive is often very useful. It is simpler to use than an Event, for example, when waiting for threads to start up, or to finish. See http://en.wikipedia.org/wiki/Barrier_(computer_science) for info. This particular implementation contains an important feature: The ability to adjust the 'count' of the barrier. This is useful in case a thread dies for some reason, to avoid a deadlock for the other threads. There is still no documentation, since this is only a proposal, but there is a unittest. |
You should probably mention that pthread_barrier and java.util.concurrent.CyclicBarrier are prior art for this. I'm thinking about them when looking at the API to see whether your differences make sense. "enter" seems to be the wrong term for this, since there's no matching "exit" call. "wait" or "block" seem better. Both pthread_barrier and CyclicBarrier provide a way to identify a unique thread from each group. pthread_barrier_wait returns true in exactly one thread, while CyclicBarrier runs a callback while all other threads are still paused. I'd be inclined to use CyclicBarrier's interface here, although then you have to define what happens when the action raises an exception. _release should notify_all after its loop. adjust_count makes me nervous. Is there a context manager interface that would make this cleaner/safer? |
I'll provide a new version shortly, targeted for the py3k branch. |
Ping -- is this something you want in 3.2, Kristjan? |
Hi, I had forgotten about this. Anyway, I´ll get my act together. Same with RWLock, that is almost ready |
Okay, here is a new submission. There is also a rather comprehensive set of tests. What is missing is documentation, somethign I shall add if this gets a positive response. Note how, in the tests, I sometimes create a "barrier2" object to facilitate external synchronization. This demonstrates the simplicity of using this primitive. Another note: In order to implement "timeout" behaviour, I changed Condition.wait() to return True in case it returns due to a timeout occurring. I folded this into this patch, but if such a change is not accepted, or we want it separately, then I'll have to remove the timeout functionality from the Barrier. I don't want to have complicated logic in there to measure time. Also, I do think that locking primitives that time out should be able to provide an indication to that fact to their callers, so condition.wait() really should do that. |
ping? |
The tests pass for me, and the patch looks good except for a stray change to Condition objects. |
Right. The condition object change is necessary to have timeout work. I can remove that feature, and slate it for another day. Add a separate patch for a Condition.wait() return value. All of the other apis are able to let the caller know whether a timeout occurred or not, I think Condition.wait() should do the same. Actually, I can fudge the timeout with time.clock(), which is good enough. I'll write up some docs. |
Well, that change would be fine by me, it was just not explained anywhere in the patch. So if it's going to be documented (with versionchanged etc.), just leave it in. |
Here is an updated patch. It contains documentation. |
Two comments:
Don't worry about markup errors, I review doc changes routinely after they are committed. |
Right. |
Looks good to me now, I think you can commit it. |
Committed as revision 85878 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: