RE: [GD-Windows] Stopping a thread cleanly
Brought to you by:
vexxed72
From: Brian S. <bs...@mi...> - 2002-02-18 19:57:14
|
Function calls have nothing do with it. "Assume aliasing across function calls" is a separate setting. This program has an aliasing problem, yet thread 2 has no function calls in it. See for yourself: #include <windows.h> #include <stdio.h> int gTrueCount, gFalseCount; DWORD WINAPI ThreadProc(LPVOID param) { bool * fooPtr =3D (bool *)param; while (1) { if (*fooPtr) { gTrueCount++; } else { gFalseCount++; } } } int main(int argc, char **argv) { gTrueCount =3D 0; gFalseCount =3D 0; bool foo =3D true; CreateThread(NULL, 0, ThreadProc, &foo, 0, NULL); while (1) { foo =3D !foo; printf("True count: %d False count: %d\n", gTrueCount, gFalseCount); ::Sleep(1000); } return 0; } > -----Original Message----- > From: Jon Watte [mailto:hp...@mi...] > Sent: Monday, February 18, 2002 10:56 AM > To: Brian Sharon; bri...@py...; gamedevlists- > wi...@li... > Subject: RE: [GD-Windows] Stopping a thread cleanly >=20 >=20 > It only needs to assume this if you're calling any other function > from within that loop. Of course, Sleep(), or WaitForSingleObject(), > or pretty much anything else you might want to do in the loop does > count as a function call (as long as it doesn't get inlined) but > I thought I'd mention it. >=20 > volatile is your friend when you're using a shared-memory thread > communication model. >=20 > Cheers, >=20 > / h+ >=20 > > The compiler should always reload it unless you've specified "assume no > > aliasing" (which is not the default, even in optimized builds). Since > > it's not a local variable, it has to assume that pData->bKill can be > > changed every time through the loop. >=20 |