Click here to Skip to main content
15,891,876 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Question about Interlocked functions Pin
cmk28-Apr-07 13:23
cmk28-Apr-07 13:23 
GeneralRe: Question about Interlocked functions Pin
HOW WHAT28-Apr-07 14:47
HOW WHAT28-Apr-07 14:47 
GeneralRe: Question about Interlocked functions Pin
cmk28-Apr-07 15:20
cmk28-Apr-07 15:20 
GeneralRe: Question about Interlocked functions Pin
HOW WHAT28-Apr-07 22:21
HOW WHAT28-Apr-07 22:21 
GeneralRe: Question about Interlocked functions Pin
cmk29-Apr-07 0:14
cmk29-Apr-07 0:14 
GeneralRe: Question about Interlocked functions Pin
HOW WHAT29-Apr-07 4:33
HOW WHAT29-Apr-07 4:33 
GeneralRe: Question about Interlocked functions Pin
cmk29-Apr-07 9:13
cmk29-Apr-07 9:13 
GeneralRe: Question about Interlocked functions Pin
HOW WHAT29-Apr-07 20:05
HOW WHAT29-Apr-07 20:05 
Yes, my code not make sense, it only write example for my question.

Is nether code right now? Which Interlocked functions unneed?


1.
volatile LONG g_Value = -1;<br />
<br />
LONG Read()<br />
{<br />
	// return g_Value;<br />
	return InterlockedExchange(&g_Value, g_Value);<br />
}<br />
<br />
VOID Write(LONG NewValue)<br />
{<br />
	// g_Value = NewValue;<br />
	InterlockedExchange(&g_Value, NewValue);<br />
}<br />
<br />
VOID Thread1()<br />
{<br />
	if (Read() >= 0)<br />
	{<br />
		Write(-1);<br />
<br />
		for (INT i = 0; i < 10; i++)<br />
		{<br />
			......<br />
			Write(i);<br />
<br />
			if (RunThread2() == error)<br />
			{<br />
				......<br />
				Write(-1);<br />
			}<br />
		}<br />
	}<br />
}<br />
<br />
VOID Thread2()<br />
{<br />
	// code same with Thread1 function<br />
}


-----------------------------------------------------

2.
volatile LONG g_nIndex = -1;<br />
<br />
VOID Thread1()<br />
{<br />
	// g_nIndex = 1;<br />
	InterlockedExchange(&g_nIndex, 1);<br />
}<br />
<br />
VOID Thread2()<br />
{<br />
	do<br />
	{<br />
		......<br />
	}<br />
	// while (g_nIndex < -1);<br />
	while (InterlockedExchange(&g_nIndex, g_nIndex) < -1);<br />
}<br />
<br />
VOID Thread3()<br />
{<br />
	INT nIndex = GetIndex();<br />
<br />
	// g_nIndex = nIndex;<br />
	InterlockedExchange(&g_nIndex, nIndex);<br />
}


-----------------------------------------------------

3.
volatile LONG g_nData1 = 0L;<br />
<br />
VOID Thread1()<br />
{<br />
	// if (0L == g_nData1) g_nData = 1;<br />
	InterlockedCompareExchange(g_nData1, 1, 0L);<br />
<br />
	.................<br />
<br />
	// if (1 == g_nData1) g_nData = 0L;<br />
	InterlockedCompareExchange(g_nData1, 0, 1);<br />
}<br />
<br />
VOID Thread2()<br />
{<br />
	// code same with Thread1<br />
}


-------------------------------------------------------

4.
volatile LONG g_bIsLock = 0L;<br />
<br />
VOID Thread1()<br />
{<br />
	// if (1 == g_bIsLock) g_bIsLock = 0;<br />
	InterlockedCompareExchange(g_nData1, 0L, 1);<br />
}<br />
<br />
VOID Thread2()<br />
{<br />
	// if (0 == g_bIsLock)<br />
	if (InterlockedExchange(&g_bIsLock, g_bIsLock) == 0L)<br />
	{<br />
		......<br />
	}<br />
}<br />
<br />
VOID Thread3()<br />
{<br />
	// if (1 == g_bIsLock)<br />
	if (InterlockedExchange(&g_bIsLock, g_bIsLock) == 1)<br />
	{<br />
		......<br />
	}<br />
}



---------------------------------------------

And can u tell me why nether MSDN example unuseInterlocked functions, but it can work right and have not resource conflict( a thread read value, the other one write value)

volatile bool Sentinel = true;<br />
<br />
unsigned ThreadFunc1( void* pArguments ) {<br />
	while (Sentinel){;}<br />
<br />
	......<br />
	return 0;<br />
} <br />
<br />
unsigned ThreadFunc2( void* pArguments ) {<br />
	Sentinel = false; // exit critical section<br />
	return 0;<br />
}

GeneralRe: Question about Interlocked functions Pin
HOW WHAT28-Apr-07 22:48
HOW WHAT28-Apr-07 22:48 
GeneralRe: Question about Interlocked functions Pin
cmk29-Apr-07 0:01
cmk29-Apr-07 0:01 
GeneralRe: Question about Interlocked functions Pin
HOW WHAT29-Apr-07 2:49
HOW WHAT29-Apr-07 2:49 
GeneralRe: Question about Interlocked functions Pin
cmk29-Apr-07 9:04
cmk29-Apr-07 9:04 
Questionhow to make very large numbers Pin
g3RC4n27-Apr-07 15:43
g3RC4n27-Apr-07 15:43 
AnswerRe: how to make very large numbers Pin
David Crow27-Apr-07 17:02
David Crow27-Apr-07 17:02 
GeneralRe: how to make very large numbers Pin
Rajesh R Subramanian27-Apr-07 19:31
professionalRajesh R Subramanian27-Apr-07 19:31 
GeneralRe: how to make very large numbers Pin
g3RC4n27-Apr-07 23:11
g3RC4n27-Apr-07 23:11 
GeneralRe: how to make very large numbers Pin
cp987628-Apr-07 0:21
cp987628-Apr-07 0:21 
GeneralRe: how to make very large numbers Pin
g3RC4n28-Apr-07 8:48
g3RC4n28-Apr-07 8:48 
QuestionLog in to website Pin
baloneyman27-Apr-07 15:42
baloneyman27-Apr-07 15:42 
QuestionRe: Log in to website Pin
Rajesh R Subramanian27-Apr-07 19:30
professionalRajesh R Subramanian27-Apr-07 19:30 
AnswerRe: Log in to website Pin
baloneyman27-Apr-07 22:02
baloneyman27-Apr-07 22:02 
Questiondatabase and CListCtrl Pin
hero199527-Apr-07 14:53
hero199527-Apr-07 14:53 
AnswerRe: database and CListCtrl Pin
David Crow27-Apr-07 17:04
David Crow27-Apr-07 17:04 
QuestionPlaySound using Add resource (MFC) Pin
YUANGE27-Apr-07 7:47
YUANGE27-Apr-07 7:47 
AnswerRe: PlaySound using Add resource (MFC) Pin
David Crow27-Apr-07 9:34
David Crow27-Apr-07 9:34 

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.