473,412 Members | 1,921 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,412 software developers and data experts.

Are Lists thread safe?

Are lists thread safe? Or do I have to use a Lock when modifying the
list (adding, removing, etc)? Can you point me to some documentation
on this?

thanks

Mar 9 '07 #1
7 16617
abcd wrote:
Are lists thread safe? Or do I have to use a Lock when modifying the
list (adding, removing, etc)? Can you point me to some documentation
on this?

thanks
You really should at least try Google first:

http://effbot.org/pyfaq/what-kinds-o...hread-safe.htm

-Larry
Mar 9 '07 #2
On Mar 9, 1:03 pm, "abcd" <codecr...@gmail.comwrote:
Are lists thread safe? Or do I have to use a Lock when modifying the
list (adding, removing, etc)? Can you point me to some documentation
on this?

thanks

Yes there are still some holes which can bite you. Adding and
removing is thread safe but don't treat the list as locked between
operations unless you specifically do your own locking. You still
need to be on the lookout for race conditions.

Greg

Mar 9 '07 #3
Thanks for the link. I saw that one in my google search but didn't
visit it for some reason.

Looks like most operations should be just fine.

Thanks.

Mar 9 '07 #4
I guess this might be overkill then...

class MyList(list):
def __init__(self):
self.l = threading.Lock()

def append(self, val):
try:
self.l.acquire()
list.append(self, val)
finally:
if self.l.locked():
self.l.release()

.....performing the same locking/unlocking for the other methods (i.e.
remove, extend, etc).

Mar 9 '07 #5
On Mar 9, 2:50 pm, "abcd" <codecr...@gmail.comwrote:
I guess this might be overkill then...

class MyList(list):
def __init__(self):
self.l = threading.Lock()

def append(self, val):
try:
self.l.acquire()
list.append(self, val)
finally:
if self.l.locked():
self.l.release()

....performing the same locking/unlocking for the other methods (i.e.
remove, extend, etc).
comments?

Mar 9 '07 #6
En Fri, 09 Mar 2007 16:50:04 -0300, abcd <co*******@gmail.comescribió:
I guess this might be overkill then...
That depends on your target. For the *current* CPython implementation,
yes, because it has an internal lock. But other versions (like Jython or
IronPython) may not behave that way.
class MyList(list):
def __init__(self):
self.l = threading.Lock()
Better to use an RLock, and another name instead of l:
self.lock = threading.RLock()
(A method may call another, and a Lock() won't allow that)
def append(self, val):
try:
self.l.acquire()
list.append(self, val)
finally:
if self.l.locked():
self.l.release()
I'd write it as:

def append(self, val):
self.lock.acquire()
try:
list.append(self, val)
finally:
self.lock.release()
....performing the same locking/unlocking for the other methods (i.e.
remove, extend, etc).
Note that even if you wrap *all* methods, operations like mylist += other
are still unsafe.

pydef f(self): self.mylist += other
....
pyimport dis; dis.dis(f)
1 0 LOAD_FAST 0 (self)
3 DUP_TOP
4 LOAD_ATTR 0 (mylist)
7 LOAD_GLOBAL 1 (other)
10 INPLACE_ADD
11 ROT_TWO
12 STORE_ATTR 0 (mylist)
15 LOAD_CONST 0 (None)
18 RETURN_VALUE

INPLACE_ADD would call MyList.__iadd__ which you have wrapped. But you
have a race condition between that moment and the following STORE_ATTR, a
context switch may happen in the middle.

It may not be possible to create an absolutely thread-safe list without
some help on the client side. (Comments, someone?)

--
Gabriel Genellina

Mar 10 '07 #7
"Gabriel Genellina" <ga*******@yahoo.com.arwrote:
INPLACE_ADD would call MyList.__iadd__ which you have wrapped. But you
have a race condition between that moment and the following
STORE_ATTR, a context switch may happen in the middle.

It may not be possible to create an absolutely thread-safe list
without some help on the client side. (Comments, someone?)
The list itself can be thread safe quite easily, but if the namespace from
which you reference it is shared between threads you would have to protect
the namespace as well, or avoid using in-place operators.

The rebinding is a mutation on the namespace rather than the object, so
that is what you have to protect.
Mar 12 '07 #8

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...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
15
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual...
1
by: jecheney | last post by:
Hi, Im currently using the following code for reading/writing to a network socket. private StreamReader clientStreamReader; private StreamWriter clientStreamWriter; .... TcpClient tcpClient...
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?
12
by: Peter K | last post by:
Say I have this class public class Simple { private string name; public string Name { get { return name; } set { name = value; }
44
by: climber.cui | last post by:
Hi all, Does anyone have experience on the thread-safty issue with malloc()? Some people said this function provided in stdlib.h is not thread- safe, but someone said it is thread safe. Is it...
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; }
29
by: NvrBst | last post by:
I've read a bit online seeing that two writes are not safe, which I understand, but would 1 thread push()'ing and 1 thread pop()'ing be thread-safe? Basically my situation is the follows: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.