#include <windows.h> #include <tchar.h> enum ThreadSign { NO1, NO2, NO3 }; ThreadSign g_ThreadSign; DWORD WINAPI ThreadFuncNo1(LPVOID lParama) { for (int i = 1; i <=100 ; ++i) { while (g_ThreadSign != NO1) { Sleep(1); } _tprintf(TEXT("No1:%d\r\n"), i); g_ThreadSign = NO2; } return 0; } DWORD WINAPI ThreadFuncNo2(LPVOID lParama) { for (int i = 101; i <= 200; ++i) { while (g_ThreadSign != NO2) { Sleep(1); } _tprintf(TEXT("No2:%d\r\n"), i); g_ThreadSign = NO3; } return 0; } int main() { HANDLE hThread[2]; hThread[0] = CreateThread(nullptr, 0, ThreadFuncNo1, nullptr, 0, nullptr); hThread[1] = CreateThread(nullptr, 0, ThreadFuncNo2, nullptr, 0, nullptr); g_ThreadSign = NO1; for (int i = 201; i <= 300; ++i) { while (g_ThreadSign != NO3) { Sleep(1); } _tprintf(TEXT("No3:%d\r\n"), i); g_ThreadSign = NO1; } WaitForMultipleObjects(2,hThread,TRUE,INFINITE); for (int i = 0; i < sizeof(hThread)/sizeof(hThread[0]); ++i) { CloseHandle(hThread[i]); } return 0; }