473,473 Members | 1,906 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Question regarding thread atomicity

If I have the following line of Python code:

self.foo.bar += 1

where 'foo' is an instance of an object and 'bar' is an integer,
is that thread-safe? i.e. will the GIL mean that it is reliable,
or do I need to manually add locking around it?
Jul 18 '05 #1
3 1314
Jon Ribbens wrote:

If I have the following line of Python code:

self.foo.bar += 1

where 'foo' is an instance of an object and 'bar' is an integer,
is that thread-safe? i.e. will the GIL mean that it is reliable,
or do I need to manually add locking around it?


Provided another thread is not trying to change the binding
for "self.foo", and provided that there is no magic __setattr__
or __getattr__ dynamically finding the "bar" attribute (or the
"foo" one, for that matter), and depending on what you mean by
"thread-safe", then things should work safely. :-)

Checking the output of "dis" is a good way to check this kind of
thing a little more:
import dis
class A: .... def func(self):
.... self.foo.bar += 1
.... dis.dis(A.func)

3 0 LOAD_FAST 0 (self)
3 LOAD_ATTR 1 (foo)
6 DUP_TOP
7 LOAD_ATTR 2 (bar)
10 LOAD_CONST 1 (1)
13 INPLACE_ADD
14 ROT_TWO
15 STORE_ATTR 2 (bar)
18 LOAD_CONST 0 (None)
21 RETURN_VALUE

Note that the STORE_ATTR part is the key part that could make the
program crash if it were not atomic, but note that there are a
few different ways that you could get wrong results if you actually
had the above code executing from several threads simultaneously,
affecting the same object.

The basic failure mode would be that two threads both retrieve
the same current value for "bar" and add one to it "INPLACE", then
store it back again: the result is that although two attempts to
add have been made, the value increases by only one. Python won't
crash, which is why this all depends on your definition of "thread-safe",
but it likely won't do what you probably want it to do.

-Peter
Jul 18 '05 #2
In article <3F***************@engcorp.com>, Peter Hansen wrote:
The basic failure mode would be that two threads both retrieve
the same current value for "bar" and add one to it "INPLACE", then
store it back again: the result is that although two attempts to
add have been made, the value increases by only one. Python won't
crash, which is why this all depends on your definition of "thread-safe",
but it likely won't do what you probably want it to do.


Ah. This violates my definition of "thread-safe" ;-) If it doesn't go
up by exactly one every time that line is executed, it isn't doing
what I want it to do. I guess I'll have to go and add a load of manual
locking :-(
Jul 18 '05 #3
Jon Ribbens <jo********@unequivocal.co.uk> writes:
Ah. This violates my definition of "thread-safe" ;-) If it doesn't go
up by exactly one every time that line is executed, it isn't doing
what I want it to do. I guess I'll have to go and add a load of manual
locking :-(


I have to agree, it looks like a pretty bad bug. The most natural way
I can think of to fix it is to add something like references to the
bytecode interpreter, so that

7 LOAD_ATTR 2 (bar)
10 LOAD_CONST 1 (1)
13 INPLACE_ADD
14 ROT_TWO
15 STORE_ATTR 2 (bar)

would become something like

LOAD_REF 2 (bar) # load a pointer to bar's location
LOAD_CONST 1
INPLACE_ADD_REF

However, apparently there's some implementation reasons to not want to
do it that way.
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Samuel | last post by:
I wrote a very simple httpmodule and tried to compile it with no success. This is my code: ============== Imports System Imports System.Web Imports Microsoft.VisualBasic NameSpace...
20
by: Sacha | last post by:
Consider I have a working thread like this, while (1) { if ( g_pObject ) g_pObject->DoOneStep(); } with an class hierarchie like this
14
by: Michi Henning | last post by:
Hi, I can't find a statement about this in the threading sections in the doc... Consider: class Class1 { Class1() { _val = 42;
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
5
by: archana | last post by:
Hi all, I am using timer to do some functionality on user specified time. I am using system.timers.timer class and its timer to do this functionality. What i am doing is i set autoreset to...
12
by: David | last post by:
Below are three classes for a console application. If put into three separate files, the sub main() will launch multiple threads adding and removing the same value. At the end we expect the value...
4
by: fred | last post by:
hi All, I am working in a multi thread project. is it necessary to use semaphore when doing reading operation. in my case I do loop to read share memory data. I remember it should be fine to read...
6
by: Sid Price | last post by:
I have been reading about the managed heap and garbage collection and I have a question. I have a class that has a very simple task and that is to send a UDP message to a remote computer. The...
13
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.