943,670 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 665
  • C++ RSS
Sep 7th, 2009
0

locking needed for simple addition?

Expand Post »
I am using multiple threads that are doing work. I need the threads to update an integer once they finish processing.

C++ Syntax (Toggle Plain Text)
  1. //Lock
  2. i_Flag++;
  3. //Unlock

Then

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dumrat is offline Offline
24 posts
since Jun 2009
Sep 7th, 2009
-7

Re: locking needed for simple addition?

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Sep 7th, 2009
0

Re: locking needed for simple addition?

You reminded me one of my interviews (that did not go that well ). Yes this is how you need to do it.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Sep 7th, 2009
0

Re: locking needed for simple addition?

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?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 7th, 2009
0

Re: locking needed for simple addition?

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dumrat is offline Offline
24 posts
since Jun 2009
Sep 7th, 2009
0

Re: locking needed for simple addition?

Click to Expand / Collapse  Quote originally posted by dumrat ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 7th, 2009
-7

Re: locking needed for simple addition?

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Sep 7th, 2009
1

Re: locking needed for simple addition?

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 26th, 2009
0

Re: locking needed for simple addition?

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jyh5 is offline Offline
4 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Interface class issue
Next Thread in C++ Forum Timeline: Windows Forms and remote thread





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC