64,652
社区成员
发帖
与我相关
我的任务
分享
DWORD WINAPI FirstThread(PVOID pvParam) {
// Initialize a stack-based variable
int x = 0;
DWORD dwThreadID;
// Create a new thread.
HANDLE hThread = CreateThread(NULL, 0, SecondThread, (PVOID) &x,
0, &dwThreadID);
// We don't reference the new thread anymore,
// so close our handle to it.
CloseHandle(hThread);
// Our thread is done.
// BUG: our stack will be destroyed, but
// SecondThread might try to access it.
return(0);
}
DWORD WINAPI SecondThread(PVOID pvParam) {
// Do some lengthy processing here. ... //
Attempt to access the variable on FirstThread's stack.
// NOTE: This may cause an access violation – it depends on timing!
* ((int *) pvParam) = 5; ... return(0);
}
unsigned long _beginthreadex(
void *security,
unsigned stack_size,
unsigned (*start_address)(void *),
void *arglist,
unsigned initflag,
unsigned *thrdaddr);