Hello,

I have a case of multithreaded program where under some condition, I
would want to test the flag of the Event object and clear it atomically.
This would allow me to know which thread has actually cleared the flag
and thus should perform some more work before setting it again.

The isSet method is of course not good enough because it's subject to
race conditions. (Not even sure the GIL would make it safe.)

The changes to threading.py would be pretty trivial:

@@ -515,8 +515,10 @@
 
         """
         with self._cond:
+            prevflag = self._flag
             self._flag = True
             self._cond.notify_all()
+        return prevflag
 
     def clear(self):
         """Reset the internal flag to false.
@@ -526,7 +528,9 @@
 
         """
         with self._cond:
+            prevflag = self._flag
             self._flag = False
+        return prevflag
 
     def wait(self, timeout=None):
         """Block until the internal flag is true.



What do you think about it?


Best regards,
Celelibi
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to