locking needed for simple addition?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 22
Reputation: dumrat is an unknown quantity at this point 
Solved Threads: 0
dumrat dumrat is offline Offline
Newbie Poster

locking needed for simple addition?

 
0
  #1
Sep 7th, 2009
I am using multiple threads that are doing work. I need the threads to update an integer once they finish processing.

  1. //Lock
  2. i_Flag++;
  3. //Unlock

Then

  1. if(i_Flag == i_ThreadCount)
  2. //Do something.

My question is whether I need to use locks around i_Flag++ because it is a simple operation.

Thank you
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,413
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1470
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: locking needed for simple addition?

 
-7
  #2
Sep 7th, 2009
>>My question is whether I need to use locks around

Yes, you need to synchronize access to that variable. There are a couple ways to do it: 1) semiphores, and 2) critical sections. Years ago I didn't do either because all it did was increment a global varialble. It cost me three days to figure out why my program randomily froze -- it was in a deadlock situation.

I would suggest you write a function to increment the variable. Inside that function you can add code to synchronize the threads. Then call that function instead of incrementing the variable all over the code.
Last edited by Ancient Dragon; Sep 7th, 2009 at 1:11 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: locking needed for simple addition?

 
0
  #3
Sep 7th, 2009
You reminded me one of my interviews (that did not go that well ). Yes this is how you need to do it.
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: locking needed for simple addition?

 
0
  #4
Sep 7th, 2009
Originally Posted by Ancient Dragon View Post
Years ago I didn't do either because all it did was increment a global varialble.
Just out of curiousity, what was the type of the variable, and on what platform? And related, ain't an integer increment on a PC on atomic operation?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 22
Reputation: dumrat is an unknown quantity at this point 
Solved Threads: 0
dumrat dumrat is offline Offline
Newbie Poster

Re: locking needed for simple addition?

 
0
  #5
Sep 7th, 2009
Originally Posted by Dave Sinkula View Post
Just out of curiousity, what was the type of the variable, and on what platform? And related, ain't an integer increment on a PC on atomic operation?
Just the reason I asked the question . It is an integer as I mentioned in the code.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: locking needed for simple addition?

 
0
  #6
Sep 7th, 2009
Originally Posted by dumrat View Post
Just the reason I asked the question . It is an integer as I mentioned in the code.
As my question was directed to Ancient Dragon, I wasn't sure that it was clear (to me) that the situation he mentioned was identical.

[edit]I'm finding related info here. [edit]And here. Probably good 'nuff from that.
Last edited by Dave Sinkula; Sep 7th, 2009 at 3:06 am.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,413
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1470
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: locking needed for simple addition?

 
-7
  #7
Sep 7th, 2009
Originally Posted by Dave Sinkula View Post
Just out of curiousity, what was the type of the variable, and on what platform? And related, ain't an integer increment on a PC on atomic operation?
16-bit MS-DOS 6.X which contained my own threading kernel, and it was an integer.

Incrementing an int may take more than one assembly instruction, and a task switch could take place during the execution of any given assembly instruction. Without some sort of locking mechanism there is no guarantee that the increment operator will finish before another thread tries to read/write it.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: locking needed for simple addition?

 
1
  #8
Sep 7th, 2009
Some additional reading, which Dave's links allude to.
http://www.itis.mn.it/linux/quarta/x86/lock.htm
http://www.dlugosz.com/Repertoire/re...hitepaper.html
http://www.informationdelight.info/e...embly_language

You're not going to be able to do this in simple ANSI C, without getting your fingers grubby with assembler, or getting the OS involved in some way.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 4
Reputation: jyh5 is an unknown quantity at this point 
Solved Threads: 0
jyh5 jyh5 is offline Offline
Newbie Poster

Re: locking needed for simple addition?

 
0
  #9
Sep 26th, 2009
Yes you need a lock. Only atomic reads (32 bits on a 32 bit machine, 64 bits on a 64 bit machine) do not need locks.

.Net does provide a simple add and subract for simple data types. Reference here:
http://msdn.microsoft.com/en-us/libr...d_members.aspx
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC