Click here to Skip to main content
15,887,267 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: What is the 32-bit form of the _InterlockedIncrement intrinsic? Pin
cmk22-Sep-09 10:24
cmk22-Sep-09 10:24 
GeneralRe: What is the 32-bit form of the _InterlockedIncrement intrinsic? Pin
Skippums22-Sep-09 10:53
Skippums22-Sep-09 10:53 
GeneralRe: What is the 32-bit form of the _InterlockedIncrement intrinsic? Pin
cmk22-Sep-09 10:59
cmk22-Sep-09 10:59 
GeneralRe: What is the 32-bit form of the _InterlockedIncrement intrinsic? Pin
Skippums22-Sep-09 11:14
Skippums22-Sep-09 11:14 
GeneralRe: What is the 32-bit form of the _InterlockedIncrement intrinsic? Pin
cmk22-Sep-09 11:55
cmk22-Sep-09 11:55 
AnswerRe: [Solved] What is the 32-bit form of the _InterlockedIncrement intrinsic? Pin
Randor 22-Sep-09 12:04
professional Randor 22-Sep-09 12:04 
GeneralRe: [Solved] What is the 32-bit form of the _InterlockedIncrement intrinsic? Pin
Skippums22-Sep-09 12:20
Skippums22-Sep-09 12:20 
GeneralRe: [Solved] What is the 32-bit form of the _InterlockedIncrement intrinsic? Pin
Randor 22-Sep-09 13:33
professional Randor 22-Sep-09 13:33 
Jeff,

A context switch[^] can occur at the top of your atomicInc function. Let me go into detail:

For example this simple program:

volatile long m_Lock;
__int32 atomicInc(volatile long * val)
{
	return _InterlockedIncrement(val);
}

int _tmain(int argc, _TCHAR* argv[])
{
	atomicInc(&m_Lock);
	return 0;
}


Lets compile with /FAs and inspect the assembler output:

//Here is the part in main where we call atomicInc

; 13   : 	atomicInc(&m_Lock);

	push	OFFSET ?m_Lock@@3JC			; m_Lock
	call	?atomicInc@@YAHPCJ@Z			; atomicInc
	add	esp, 4


//Here is your atomicInc function:
atomicInc@@YAHPCJ@Z PROC				; atomicInc, COMDAT

; 7    : {

	push	ebp
	mov	ebp, esp
	sub	esp, 64					; 00000040H
	push	ebx
	push	esi
	push	edi

//Now look at all the instructions that are executed before you attempt to atomically increment your variable.
//A context switch CAN take place within this small window your count will NOT be atomic.
//In fact maybe another thread modified it and will cause a deadlock or race condition.
; 8    : 	return _InterlockedIncrement(val);

	mov	eax, DWORD PTR _val$[ebp]
	mov	ecx, 1
	lock	 xadd	 DWORD PTR [eax], ecx
//The line above is the atomic increment instructions.
	inc	ecx
	mov	eax, ecx

; 9    : }

	pop	edi
	pop	esi
	pop	ebx
	mov	esp, ebp
	pop	ebp
	ret	0


The _InterlockedIncrement functions should not be wrapped. By wrapping them you are potentially losing atomicity. You should pray that your compiler optimizes your code and removes the atomicInc function call. (It probably will in Release mode) But keep in mind your code is not atomic at all. Here is what I am saying in laymens terms:

1.) The code you presented is not atomic.
2.) The compiler may optimize and fix the bug you have created.

Good Luck,
-David Delaune
GeneralRe: [Solved] What is the 32-bit form of the _InterlockedIncrement intrinsic? Pin
Skippums22-Sep-09 14:00
Skippums22-Sep-09 14:00 
QuestionGreyscale image from raw data Pin
David Ferreira22-Sep-09 4:57
David Ferreira22-Sep-09 4:57 
AnswerRe: Greyscale image from raw data Pin
includeh1022-Sep-09 5:20
includeh1022-Sep-09 5:20 
AnswerRe: Greyscale image from raw data Pin
enhzflep22-Sep-09 20:02
enhzflep22-Sep-09 20:02 
GeneralRe: Greyscale image from raw data Pin
David Ferreira22-Sep-09 21:26
David Ferreira22-Sep-09 21:26 
GeneralRe: Greyscale image from raw data Pin
enhzflep22-Sep-09 23:15
enhzflep22-Sep-09 23:15 
GeneralRe: Greyscale image from raw data Pin
David Ferreira23-Sep-09 0:58
David Ferreira23-Sep-09 0:58 
AnswerRe: Greyscale image from raw data Pin
David Ferreira19-Nov-09 23:30
David Ferreira19-Nov-09 23:30 
QuestionCreation of install shield Pin
shir_k22-Sep-09 3:55
shir_k22-Sep-09 3:55 
AnswerRe: Creation of install shield Pin
PJ Arends22-Sep-09 4:55
professionalPJ Arends22-Sep-09 4:55 
GeneralRe: Creation of install shield Pin
shir_k4-Oct-09 21:24
shir_k4-Oct-09 21:24 
Questionhow to convert char * to char[100] Pin
prithaa22-Sep-09 3:54
prithaa22-Sep-09 3:54 
AnswerRe: how to convert char * to char[100] Pin
Chris Losinger22-Sep-09 4:10
professionalChris Losinger22-Sep-09 4:10 
AnswerRe: how to convert char * to char[100] Pin
Rajesh R Subramanian22-Sep-09 4:30
professionalRajesh R Subramanian22-Sep-09 4:30 
Generalagree! Pin
etkid8422-Sep-09 5:55
etkid8422-Sep-09 5:55 
GeneralRe: agree! Pin
Rajesh R Subramanian22-Sep-09 6:01
professionalRajesh R Subramanian22-Sep-09 6:01 
Generaloops, my bad Pin
etkid8422-Sep-09 6:10
etkid8422-Sep-09 6:10 

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.