Alright then, consider this:
Code:
HANDLE hEvent;
void foo2(pp<int>& pTest)
{
SetEvent(hEvent); // Tell main thread to continue
Sleep(1000); // Simulate doing some work for 1s
int n = *pTest;
n;
}
void foo3(pp<int> pTest)
{
SetEvent(hEvent); // Tell main thread to continue
Sleep(1000); // Simulate doing some work for 1s
int n = *pTest;
n;
}
DWORD WINAPI foo(void* pParameter) // Wrapper function; takes param and passes it to our processing functions
{
// Thread entry. Fetch variable we passed along and pass it on to our processing functions.
ppnew<int>* p = (ppnew<int>*)pParameter;
foo2(*p);
//foo3(*p);
return 0;
}
BOOL CKantanAnimeApp::InitInstance()
{
//foo_s* ps = new foo_s;
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
ppnew<int> OurMemory; // The memory we're fighting over
*OurMemory = 100;
//NewThread(&CKantanAnimeApp::foo, this, ps->pint);
::CreateThread(NULL, NULL, &foo, &OurMemory, NULL, NULL); // Begin race condition
WaitForSingleObject(hEvent, INFINITE); // Wait for the new thread to be ready
/* Sleep(1000); // Simulate some work for 1s*/
int n = *OurMemory;
//delete ps;
OurMemory = ppnew<int>; // We require some new memory now and have no use for the old memory, so allocate some new and discard the old.
Sleep(100000); // Keep sleeping so the rest of the code won't execute, because it's not part of this example.
n;
...
}
Some argument, same code with a somewhat different way of doing it but still the same result. foo2 doesn't work, foo3 does work.