It is possible that when B calls ResumeThread , the variable C contains an uninitialized value.
However, the current reason your code does not work is because GetCurrentThread returns only a pseudo-thread descriptor, the value of which is interpreted as meaning the current thread descriptor. To get a real one that can be used from other threads, you can take one of the return of the first call to CreateThread or convert the pseudo-descriptor using the DuplicateHandle .
Change Using method 1:
HANDLE C; DWORD WINAPI A (LPVOID in) { cout << "1"; SuspendThread (C); cout << "4"; return 0; } DWORD WINAPI B (LPVOID in) { Sleep (200); cout << "2"; ResumeThread ((HANDLE)in); cout << "3"; return 0; } int main() { C = CreateThread (NULL, 0, A, NULL, 0, NULL); CreateThread (NULL, 0, B, (LPVOID)C, 0, NULL); Sleep (INFINITE); return 0; }
Actually there is another problem with your code, which is that the descriptors returned from CreateThread are ignored when they should be closed. There is also a drawback to error checking, but I assumed that you skipped this for brevity.
It should also be noted that depending on the context switching time, the following code may actually be output:
1243
Using method 2:
HANDLE C = NULL; DWORD WINAPI A (LPVOID in) { C = GetCurrentThread(); DuplicateHandle( GetCurrentProcess(), C, GetCurrentProcess(), &C, 0, FALSE, DUPLICATE_SAME_ACCESS ); cout << "1"; SuspendThread (C); cout << "4"; return 0; } DWORD WINAPI B (LPVOID in) { Sleep (200); cout << "2"; while( C == NULL ) { Sleep(100); } ResumeThread(C); cout << "3"; return 0; } int main() { CreateThread (NULL, 0, A, NULL, 0, NULL); CreateThread (NULL, 0, B, NULL, 0, NULL); Sleep (INFINITE); return 0; }