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

Do I need a lock here?

Hey,

Please take a look at the code of the two threads below:

COMMON_DICT = {}

def thread_1():
global COMMON_DICT
local_dict = prepare_dict()
COMMON_DICT = local_dict

def thread_2():
global COMMON_DICT
local_dict = COMMON_DICT
use_dict(local_dict)

Do I need a lock to protect the COMMON_DICT dictionary? AFAIK bytecode
operations are atomic and in each thread there's only one crucial
bytecode op: STORE_NAME in the first thread and LOAD_NAME in the
second one. So I suspect that everything will work just fine. Am I
right?

Regards,

Mike
Oct 27 '08 #1
2 1068
jasiu85 <ja*****@gmail.comwrote:
Hey,

Please take a look at the code of the two threads below:

COMMON_DICT = {}

def thread_1():
global COMMON_DICT
local_dict = prepare_dict()
COMMON_DICT = local_dict

def thread_2():
global COMMON_DICT
local_dict = COMMON_DICT
use_dict(local_dict)

Do I need a lock to protect the COMMON_DICT dictionary? AFAIK bytecode
operations are atomic and in each thread there's only one crucial
bytecode op: STORE_NAME in the first thread and LOAD_NAME in the
second one. So I suspect that everything will work just fine. Am I
right?
Possibly, but without further information it is impossible to tell.

You have one thing wrong though: bytecode operations are not all atomic.
For example STORE_NAME will remove a reference to the object previously
stored under a name and that could trigger code in a __del__ method or a
weak reference callback. That callback code will execute in the same thread
as the STORE_NAME, but while it is executing you could get a context switch
to another thread.

None of that will prevent the store working, and it may not be at all
applicable to your data structures, but in general any operation which can
release complex objects is not thread safe.

--
Duncan Booth http://kupuguy.blogspot.com
Oct 27 '08 #2
jasiu85 wrote:
Do I need a lock to protect the COMMON_DICT dictionary? AFAIK bytecode
operations are atomic and in each thread there's only one crucial
bytecode op: STORE_NAME in the first thread and LOAD_NAME in the
second one. So I suspect that everything will work just fine. Am I
right?
Will it crash your system? Probably not. Will you end up with correct values
in your dict? Probably not, since you can still end up with race hazards.

If you want to share data that way, you may want to look at Kamaelia's
STM[1] model, which is described on this page:

* http://www.kamaelia.org/STM

You can created stores - which are /essentially/ (key value) threadsafe
stores which enable you to figure out when things go wrong, so you can do
something about it ! :)

A short example:

from Axon.STM import Store

S = Store() # Shared store (like your COMMON_DICT)

D = S.using("account_one", "account_two", "myaccount")
D["account_one"].set(50)
D["account_two"].set(100)
D.commit() # This will fail if any of the values have changed
S.dump()

D = S.using("account_one", "account_two", "myaccount")
D["myaccount"].set(D["account_one"].value+D["account_two"].value)
D["account_one"].set(0)
D["account_two"].set(0)
D.commit() # This will fail if any of the values have changed
S.dump()

If this looks familiar, it's the same basic idiom as version control. The
nice thing of course, is in this version you don't need to worry about
locks, but just be aware that the .commit() may fail, and you may need to
retry.

Two examples - one using bare threads, one using Kamaelia components are
here:
* http://kamaelia.googlecode.com/svn/t.../Examples/STM/

Crucially whilst the above says "this will fail", the examples in that
directory handle the failures correctly :)

This code is included in the latest Kamaelia release, described here:
* http://www.kamaelia.org/GetKamaelia

However you can also use the standalone tarball attached to that page.

[1] ie a minimal software transactional memory implementation. The point of
these things really is to allow you to detect when something has gone
wrong (eg and update fails) and to redo the thing that led to the
update. It's conceptually very similar to version control for variables.
I'm tempted to replace "using" with "checkout" to mirror "commit".

Michael.
--
http://www.kamaelia.org/GetKamaelia

Oct 27 '08 #3

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

Similar topics

1
by: Rohit Raghuwanshi | last post by:
Hello all, we are running a delphi application with DB2 V8.01 which is causing deadlocks when rows are being inserted into a table. Attaching the Event Monitor Log (DEADLOCKS WITH DETAILS) here....
7
by: Sunny | last post by:
Hi, I can not understend completely the lock statement. Actally what is locked: 1. the part of the code between {...} or 2. the object in lock() In the docs is written: for 1: The lock...
4
by: Peter Lin | last post by:
Dear all; I need to monitor a xml file so that I can update my data in hashtable, but the problem is it will trigger more than one event for a lastwrite action(I trigger this action by add...
14
by: Sharon | last post by:
Hi all. I have an ArrayList and sometimes while enumerating through, i get an exception because another thread has added to the ArrayList. To solve this problem, i lock the enumeration, passing...
4
by: Scott Johnson | last post by:
Hi I am converting some code from C# to VB.NET and I have come across a command that I can't find the VB equivalent. The C# command is 'lock' and I think it is used to lock a data type from...
1
by: charlies224 | last post by:
Hi, I am writting a software that requires me to make sure the Num Lock is always on and Caps Lock is always off. First, I know how to detect if Num Lock or Caps Lock is on or off (if...
0
by: Alvin A. Delagon | last post by:
I have a multithreaded application that spawns threads which query a database server. During stress test I encountered some threads failing due "lost connection errors" and sometimes the actual...
13
by: David | last post by:
Hi all, I have a singleton ienumerable that collects data from a database. I have listed the code below. It has the usual methods of current, move and reset. I need to go to a certain position...
190
by: blangela | last post by:
If you had asked me 5 years ago about the future of C++, I would have told you that its future was assured for many years to come. Recently, I have been starting to wonder. I have been teaching...
20
by: Kurt | last post by:
Below is a class that can accessed from multiple threads and I want the class to be thread safe. I have a private timer member whose interval can be changed by different threads. Which is the correct...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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:
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
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.