Click here to Skip to main content
16,017,788 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: managed/unmanaged code Pin
Mark Salsbery4-Jun-07 6:19
Mark Salsbery4-Jun-07 6:19 
Questionquestion about function InterlockedIncrement() Pin
includeh102-Jun-07 5:50
includeh102-Jun-07 5:50 
AnswerRe: question about function InterlockedIncrement() Pin
Mark Salsbery2-Jun-07 6:00
Mark Salsbery2-Jun-07 6:00 
AnswerRe: question about function InterlockedIncrement() Pin
toxcct2-Jun-07 6:02
toxcct2-Jun-07 6:02 
Generalnot really Pin
includeh102-Jun-07 7:04
includeh102-Jun-07 7:04 
GeneralRe: not really Pin
Mark Salsbery2-Jun-07 8:16
Mark Salsbery2-Jun-07 8:16 
GeneralRe: not really Pin
includeh102-Jun-07 16:47
includeh102-Jun-07 16:47 
GeneralRe: not really Pin
Mark Salsbery3-Jun-07 9:07
Mark Salsbery3-Jun-07 9:07 
includeh10 wrote:
1) do you mean : if first thread calls InterlockedIncrement(&i_Lock) first, then second thread calls i_Lock++;
the i_Lock++ still works?


Yes. All the threads incrementinging i_Lock should use InterlockedIncrement() to do so. That's
the only way the operation will be thread safe.

includeh10 wrote:
2) what does "same time" mean?
do u mean: if InterlockedIncrement(&i_Lock); are called at "same time" by two threads, only one call works?
but, a PC has only one CPU, it can not change a value at "same time" by 2 threads in any way (++ or InterlockedIncrement).


While it's true it won't occur at the "same time" on a single-processor machine, it's best to
assume that logically it can. You have no control over when the system is going to switch threads.
If the switch occurs right in the middle of a thread changing the variable and another thread
takes over and changes that variable then the first thread resumes but the variable has changed.
That can be bad.

In the simple case of incrementing a long, chances are pretty good these days that it will take
a single machine-code instruction to do the increment. Theoretically one wouldn't need
to use thread synchronization because the active thread isn't going to change in the middle
of a CPU instruction.

InterlockedIncrement() does more than just increment though. It also checks the result and
returns the result. Now imagine doing all those operations without thread synchronization:

1) i_Lock = 0
2) Thread A increments i_Lock - i_Lock now == 1
3) Thread B becomes the executing thread
4) Thread B increments i_Lock - i_Lock now == 2
5) Thread B gets the value of i_Lock (2)
6) Thread A becomes the executing thread
7) Thread A gets the value of i_Lock (2)

i_Lock should have been 1 in thread A but thread B changed it before thread A was finished.


Now do the same with proper synchronization:

1) i_Lock = 0
2) Thread A calls InterlockedIncrement() to increment i_Lock
3) Thread B becomes the executing thread
4) Thread B calls InterlockedIncrement() to increment i_Lock
5) Thread B is put into a waiting state (paused) until Thread A's call to InterlockedIncrement()
is completed
6) Thread A becomes the executing thread
7) Thread A's call to InterlockedIncrement() completes, returning 1 (i_Lock == 1)
8) Thread B becomes the executing thread
9) Thread B's call to InterlockedIncrement() is allowed to continue
10) Thread B's call to InterlockedIncrement() completes, returning 2 (i_Lock == 2)

That's the expected behavior - first-come, first-serve Smile | :) Make sense?

InterlockedIncrement() and InterlockedDeccrement() are often used for object reference count
variables. When an object's reference count is 0, often the object should be destroyed.
Imagine not synchronizing this process (as described above). Two threads try to decrement the
ref count on an object, but both think the ref count is 0 after they have done so. Both threads
try to destroy the object - one succeeds, one crashes.

Mark




"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

QuestionFatal Error Pin
mahesh.mundackal2-Jun-07 2:55
mahesh.mundackal2-Jun-07 2:55 
QuestionRe: Fatal Error Pin
Rajesh R Subramanian2-Jun-07 3:18
professionalRajesh R Subramanian2-Jun-07 3:18 
AnswerRe: Fatal Error Pin
S Douglas2-Jun-07 19:49
professionalS Douglas2-Jun-07 19:49 
AnswerRe: Fatal Error Pin
Hamid_RT2-Jun-07 20:31
Hamid_RT2-Jun-07 20:31 
QuestionAligning Toolbars Pin
Anurag Gandhi2-Jun-07 2:23
professionalAnurag Gandhi2-Jun-07 2:23 
QuestionProblem in Inserting ActiveX Control in SDI Pin
S_Murali2-Jun-07 2:03
S_Murali2-Jun-07 2:03 
GeneralRe: Problem in Inserting ActiveX Control in SDI Pin
Rajesh R Subramanian2-Jun-07 2:09
professionalRajesh R Subramanian2-Jun-07 2:09 
GeneralRe: Problem in Inserting ActiveX Control in SDI Pin
S_Murali2-Jun-07 2:32
S_Murali2-Jun-07 2:32 
JokeRe: Problem in Inserting ActiveX Control in SDI Pin
Hamid_RT2-Jun-07 20:28
Hamid_RT2-Jun-07 20:28 
QuestionRe: Problem in Inserting ActiveX Control in SDI Pin
Hamid_RT2-Jun-07 20:27
Hamid_RT2-Jun-07 20:27 
QuestionException! Pin
kcynic2-Jun-07 1:05
kcynic2-Jun-07 1:05 
GeneralRe: Exception! Pin
Rajesh R Subramanian2-Jun-07 1:11
professionalRajesh R Subramanian2-Jun-07 1:11 
GeneralRe: Exception! Pin
kcynic2-Jun-07 1:27
kcynic2-Jun-07 1:27 
GeneralRe: Exception! Pin
Rajesh R Subramanian2-Jun-07 1:33
professionalRajesh R Subramanian2-Jun-07 1:33 
GeneralRe: Exception! Pin
kcynic2-Jun-07 1:45
kcynic2-Jun-07 1:45 
GeneralRe: Exception! Pin
Rajesh R Subramanian2-Jun-07 2:00
professionalRajesh R Subramanian2-Jun-07 2:00 
AnswerRe: Exception! Pin
Matthew Faithfull2-Jun-07 1:59
Matthew Faithfull2-Jun-07 1:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.