| | |
locking needed for simple addition?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2009
Posts: 22
Reputation:
Solved Threads: 0
I am using multiple threads that are doing work. I need the threads to update an integer once they finish processing.
Then
My question is whether I need to use locks around i_Flag++ because it is a simple operation.
Thank you
C++ Syntax (Toggle Plain Text)
//Lock i_Flag++; //Unlock
Then
C++ Syntax (Toggle Plain Text)
if(i_Flag == i_ThreadCount) //Do something.
My question is whether I need to use locks around i_Flag++ because it is a simple operation.
Thank you
>>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.
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.
"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
•
•
•
•
Just the reason I asked the question. It is an integer as I mentioned in the code.
[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
•
•
•
•
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?
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.
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.
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.
•
•
Join Date: Sep 2008
Posts: 4
Reputation:
Solved Threads: 0
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
.Net does provide a simple add and subract for simple data types. Reference here:
http://msdn.microsoft.com/en-us/libr...d_members.aspx
![]() |
Other Threads in the C++ Forum
- Previous Thread: Interface class issue
- Next Thread: Windows Forms and remote thread
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







). Yes this is how you need to do it.
. It is an integer as I mentioned in the code.