[Python-ideas] Extending language syntax

Andrew Barnert abarnert at yahoo.com
Tue Nov 12 21:34:13 CET 2013


On Nov 12, 2013, at 10:25, Charles-François Natali <cf.natali at gmail.com> wrote:

> Of course. I probably wasn't clear, but I was actually referring to this part:
> 
>> Is there any implementation (like one of the PyPy sub projects) that uses refcounting, with interlocked increments if two interpreter threads are live but plain adds otherwise?
> 
> I'm not sure how one would do this without using locking/memory
> barriers (hence a large performance overhead).

A global "multithreading" flag. When you start a thread, it sets the flag in the parent thread, before starting the new thread. Both are guaranteed to see the True value. If there are any other threads, the value was already True.

You probably don't ever need it to go back to False--not many programs are multithreaded for a while and then single-threaded again. But if you need this, note that it's never a problem to see a spurious True, it just means you do an atomic read when you didn't need to. But also, you only need to check whether you're the last thread while returning from join, and there's no way anyone could have created another thread between the OS-level join and the end of Thread.join.



More information about the Python-ideas mailing list