Preventing tread collisions

Ian Kelly ian.g.kelly at gmail.com
Thu Dec 13 11:31:17 EST 2012


On Thu, Dec 13, 2012 at 3:26 AM, Alexander Blinne <news at blinne.net> wrote:
> I have a general question about this kinds of things. I see that the
> above is a common use case for some kind of lock which does this
> testing/locking atomically. But the question is: if I know for sure that
> there is no other thread that might get in the way this solution would
> be fine, right?

If you know for sure that there is no other thread, then there is no
need for the flag in the first place.

> In one of my applications i have a somewhat different case: i have a
> list of objects and call the same method of each object, each in its own
> thread (which is created and later joined just for this purpose). The
> objects are thus only used by that one thread, the main thread waits for
> all threads to be finished before accessing those objects again. Do i
> really need some kind of locking for those objects?

No, if a resource is only ever used by one thread/process, then there
is no reason to lock it.



More information about the Python-list mailing list