// gcc -mno-cygwin -Wall suspend.cpp -o suspend_mingw.exe -lstdc++ // cl /EHsc /Zi /I C:/cygwin/usr/local/tools/i686_win32/vc7/Vc7/include /I C:/cygwin/usr/local/tools/i686_win32/vc7/Vc7/PlatformSDK/Include suspend.cpp /link /libpath:C:/cygwin/usr/local/tools/i686_win32/vc7/Vc7/lib /libpath:C:/cygwin/usr/local/tools/i686_win32/vc7/Vc7/PlatformSDK/Lib #include #include struct counter { int count; int done; }; DWORD WINAPI Counter(LPVOID addr) { counter *c = reinterpret_cast(addr); while (!c->done) { ++c->count; } return c->count; } int main(int argc, char *argv[]) { counter c = { 0, 0 }; DWORD id; HANDLE h = CreateThread(NULL, 0, Counter, &c, 0, &id); if (h == NULL) { std::cerr << "CreateThread(): " << GetLastError() << std::endl; exit(1); } Sleep(1000); if (SuspendThread(h) < 0) { std::cerr << "SuspendThread(): " << GetLastError() << std::endl; exit(1); } int early = c.count; Sleep(1000); int late = c.count; c.done = 1; if (ResumeThread(h) < 0) { std::cerr << "ResumeThread(): " << GetLastError() << std::endl; exit(1); } DWORD final; if (!GetExitCodeThread(h, &final)) { std::cerr << "GetExitCodeThread(): " << GetLastError() << std::endl; exit(1); } if (early != late) { std::cout << early << ", " << late << std::endl; exit(2); } // If I just put "return 0;" here, I get random crashes in the exit // code when I run three parallel "repeat 100 suspend.exe" jobs. exit(0); }