[Python-ideas] Add faster locks to the threading module?
sturla at molden.no
Wed Jul 21 05:59:11 CEST 2010
Den 21.07.2010 03:58, skrev Jesse Noller:
> I see what you're trying to do (impersonating the synchronized keyword
> java has) but I'm a little skeeved out by adding anything like this
> which is directly reliant on the GIL's existence.
It is not reliant on the GIL. Sorry if you got that impression.
In a GIL free world, a global spinlock would serve the same purpose (cf.
Java). But as there is a GIL in Python, we cannot use a spinlock to
avoid the overhead of a mutex. But we can temporarily hold on to the GIL
instead, and achieve the same effect. This is very close to Java's
synchronized keyword, yes.
The main reason is that most synchronizations in multi-threaded apps are
used to protect very small pieces of code. A mutex is overkill for that.
1. release gil.
2. acquire lock.
3. re-acquire gil.
4. release lock.
we could just:
1. keep the gil for a litte while.
Also note that in the single-threaded case, the overhead from this
"synchronized" block would be close to zero. It would do nothing, except
write to the address of a volatile int.
More information about the Python-ideas