By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,595 Members | 1,433 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,595 IT Pros & Developers. It's quick & easy.

thread lock object.

P: n/a
Hello,

I have multi thread program running together and each will increment
int i.

How can we make only one thread at a time be able to access and
increment i ?

Thanks in advance,

Sincerely Yours,
pujo

Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
aj****@gmail.com wrote:
Hello,

I have multi thread program running together and each will increment
int i.

How can we make only one thread at a time be able to access and
increment i ?

Thanks in advance,

Sincerely Yours,
pujo


Use a synchronization primitive such as a lock
(threading.Lock, threading.RLock)

But for simply incrementing a number (i+=1) this is not needed
because that operation cannot be interrupted by another thread,
as far as I know.

--Irmen

Jul 19 '05 #2

P: n/a
thanks.

Pujo

Jul 19 '05 #3

P: n/a
Irmen de Jong wrote:
aj****@gmail.com wrote:
How can we make only one thread at a time be able to access and
increment i ?


Use a synchronization primitive such as a lock
(threading.Lock, threading.RLock)

But for simply incrementing a number (i+=1) this is not needed
because that operation cannot be interrupted by another thread,
as far as I know.


Most assuredly it can:
def f(): .... global x
.... x += 1
.... dis.dis(f)

3 0 LOAD_GLOBAL 0 (x)
3 LOAD_CONST 1 (1)
6 INPLACE_ADD
....a context-change here can lead to incorrect results...
7 STORE_GLOBAL 0 (x)
10 LOAD_CONST 0 (None)
13 RETURN_VALUE

-Peter
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.