8 ms·
Could we not have a per-object lock, but only on objects the programmer defines as requiring thread safety?
by xlevus 14y ago
Could we not have a per-object lock, but only on objects the programmer defines as requiring thread safety?
- jbert 14y agoIf you tag objects as being either "owned" by a thread or "shared" then you could (in the python implementation) do something like: if (obj.owner == current_thread): # just get in and mess with it elsif (obj.owner == shared): # get in and mess with it under a lock else throw "not your obj - make this obj shared if you need that" From a language perspective, you'd just need a way to declare that an object was 'shared'. All other objs would be owned by their creating thread.
- anacrolix 14y agoIt's not that easy.
- exDM69 14y agoNo, that doesn't work. Java has something like this (synchronized methods) but they are cumbersome (I find programming nicer with plain old locks and conditions, the Java synchronized model feels just a layer of cruft but its all lock&cond inside). But Python is different than Java. The Python interpreter has to guard the internal data structures of the interpreter itself, and that is a lot more tricky than it seems on the surface.