1 #define _WIN32_WINNT 0x400 2 3 #include "test.h" 4 5 static int washere = 0; 6 func(void * arg)7static void * func(void * arg) 8 { 9 washere = 1; 10 11 Sleep(1000); 12 13 return 0; 14 } 15 16 static void anotherEnding()17anotherEnding () 18 { 19 /* 20 * Switched context 21 */ 22 washere++; 23 24 pthread_exit(0); 25 } 26 27 int main()28main() 29 { 30 pthread_t t; 31 HANDLE hThread; 32 33 assert(pthread_create(&t, NULL, func, NULL) == 0); 34 35 hThread = pthread_gethandle (t); 36 37 Sleep(500); 38 39 SuspendThread(hThread); 40 41 if (WaitForSingleObject(hThread, 0) == WAIT_TIMEOUT) 42 { 43 /* 44 * Ok, thread did not exit before we got to it. 45 */ 46 CONTEXT context; 47 48 context.ContextFlags = CONTEXT_CONTROL; 49 50 GetThreadContext(hThread, &context); 51 #ifdef _M_X64 52 context.Rip = (uintptr_t) anotherEnding; 53 #else 54 context.Eip = (uintptr_t) anotherEnding; 55 #endif 56 57 SetThreadContext(hThread, &context); 58 ResumeThread(hThread); 59 } 60 else 61 { 62 printf("Exited early\n"); 63 fflush(stdout); 64 } 65 66 Sleep(1000); 67 68 assert(washere == 2); 69 70 return 0; 71 } 72 73