473,395 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Thread Safety

Are atomic operations thread safe in Python? Can immutable objects,
such as, strings, be created on one thread and then accessed by
another thread, without using locks?

Such operations are not guaranteed thread safe in Java due to caching
optimizations.

If someone could point me to reference documentation on this, that
would be great.
Jul 18 '05 #1
5 7245
MetalOne wrote:
Are atomic operations thread safe in Python?
I don't understand the question. Atomic operations, by nature, can
either completely succeed or completely fail - otherwise it would not
be atomic. This is independent of Python.

In general, you cannot cause inconsistency of Python's internal
representation of objects by pure Python code executed in multiple
threads, so Python itself is thread-safe.
Can immutable objects,
such as, strings, be created on one thread and then accessed by
another thread, without using locks?
Yes.
Such operations are not guaranteed thread safe in Java due to caching
optimizations.


But then, in Java, strings are not immutable, no?

Regards,
Martin

Jul 18 '05 #2
"Martin v. Löwis" wrote:
...
But then, in Java, strings are not immutable, no?


Nope: strings ARE immutable in Java .
Alex

Jul 18 '05 #3
"Martin v. Löwis" <ma****@v.loewis.de> wrote in message news:<bl*************@news.t-online.com>...
MetalOne wrote:
Are atomic operations thread safe in Python?


I don't understand the question. Atomic operations, by nature, can
either completely succeed or completely fail - otherwise it would not
be atomic. This is independent of Python.

But then, in Java, strings are not immutable, no?

Regards,
Martin


The problem in Java is one of visibility. Assigning
s = "hello"
does not necessarily happen if < s > is not used in the current
thread.

Likewise, another thread may see no reason to examine < s >, if < s >
has not been changed in this thread.

I found the following on a Java site.
# the language definition ensures that single memory moves concerning
32-bit values (int, etc.) are "atomic" (not interrupted)
# moving a 64-bit value (say, long) can be preempted in the middle of
the transfer operation (but it may not!)

# however, the atomicity of 32-bit moves ensures nothing about the
visibility of updated (lastly written) values (see below)

# the atomicity and correct visibility of a scalar (32- or 64-bit)
variable can be ensured by defining it volatile
# a volatile variable is always kept up-to-date and atomically loaded
from and stored into the main memory
# unfortunately, not all current Java VM implementations handle
volatile variables correctly
Java strings are immutable. There is a StringBuffer class that is
mutable.
Jul 18 '05 #4
jc*@iteris.com (MetalOne) writes:
The problem in Java is one of visibility. Assigning
s = "hello"
does not necessarily happen if < s > is not used in the current
thread.


I see. Python does guarantee that assignments are visible in other
threads once they have completed. It also guaranteed that the thread
either sees the old or the new value, i.e. assignments are atomic
(unless an object implements __setitem__).

Regards,
Martin
Jul 18 '05 #5
"Martin v. Löwis" wrote:
MetalOne wrote:
Are atomic operations thread safe in Python?


I don't understand the question. Atomic operations, by nature, can
either completely succeed or completely fail - otherwise it would not
be atomic. This is independent of Python.

In general, you cannot cause inconsistency of Python's internal
representation of objects by pure Python code executed in multiple
threads, so Python itself is thread-safe.


For the namespaces in Jython this 'Python internal thread safety'
is handled by the Java class:

http://www.jython.org/docs/javadoc/o...StringMap.html

which has almost all of it public methods Java synchronized:

http://cvs.sourceforge.net/viewcvs.p...StringMap.java

Thinking about it: is this the Jython parallel of CPython's
Global Interpreter Lock, the famous GIL?

Regards,
Ype

--
email at xs4all.nl
Jul 18 '05 #6

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

Similar topics

4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
9
by: Alexander Fleck | last post by:
Hi, I' ve to make a software module thread safe. I know how to realize that and what' re the main topics of thread safety. But I don' t know how thread safety can be tested. I read about a test...
4
by: The Crow | last post by:
for example i have static readonly SqlParameter and i want to clone them at runtime. as clone operation will not write to SqlParameter object, just reading, should i lock that object during read...
22
by: Brett | last post by:
I have a second thread, t2, that errors out and will stop. It's status is then "Stopped". I try to start t2 from thread 1, t1, by checking If t2.threadstate = "Stopped" Then t2.start() ...
4
by: Warren Sirota | last post by:
Hi, I've got a method that I want to execute in a multithreaded environment (it's a specialized spider. I want to run a whole bunch of copies at low priority as a service). It works well running...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
1
by: paul.hester | last post by:
Hi all, All of the classes in my DAL are static, with constants defining the stored procedures and parameters. I've been having some problems with my site which makes me wonder if there's a...
13
by: arun.darra | last post by:
Are the following thread safe: 1. Assuming Object is any simple object Object* fn() { Object *p = new Object(); return p; } 2. is return by value thread safe?
0
by: Graham Wideman | last post by:
Folks: Can anyone tell me what controls php's "thread safety" feature? I have an installation where phpinfo() is showing Thread safety: enabled, whereas I need it disabled in order to work...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.