#include <Windows.h> #include <iostream> using namespace std;
DWORD WINAPI ThreadIdle(LPVOID lpParam) { ?? ?int i = 0; ?? ?while (i < 10) ?? ?{ ?? ??? ?cout<<"Idle thread running, count = "<< i++<<endl; ?? ?} ?? ?return 0; }
DWORD WINAPI ThreadNormal(LPVOID lpParam) { ?? ?int i = 0; ?? ?while (i < 10) ?? ?{ ?? ??? ? ?? ??? ?cout<<"Normal thread running, count ="<<i++<<endl; ?? ?} ?? ?return 0; }
int main(int argc )//visual studio 平台
//int _tmain(int argc, _TCHAR* argv[])//c++ builder 平台 { ?? ?HANDLE h[2]; ?? ?DWORD dwThreadID;
?? ?//h[0] = ::CreateThread(NULL, NULL, ThreadIdle, NULL, CREATE_SUSPENDED, &dwThreadID); ?? ?h[0] = CreateThread(NULL, NULL, ThreadIdle, NULL, CREATE_SUSPENDED, &dwThreadID); ?? ?::SetThreadPriority(h[0], THREAD_PRIORITY_IDLE); ?? ?::ResumeThread(h[0]);
?? ?h[1] = ::CreateThread(NULL, NULL, ThreadNormal, NULL, 0, &dwThreadID);
?? ?//::WaitForMultipleObjects(2, h, TRUE, INFINITE);. ?? ?DWORD dw = ::WaitForMultipleObjects(2, h, FALSE, 0); ?? ?switch (dw) ?? ?{ ?? ?case WAIT_FAILED: ?? ??? ?cout<<"wait failed!"<<endl; ?? ??? ?break; ?? ?case WAIT_TIMEOUT: ?? ??? ?cout<<"wait timeout!"<<endl; ?? ??? ?break; ?? ?case WAIT_OBJECT_0 + 0: ?? ??? ?cout <<"HANDLE"<<h[0]<<"is running!" << endl; ?? ??? ?break; ?? ?case WAIT_OBJECT_0 + 1: ?? ??? ?cout<<"HANDLE"<< h[1]<<"is running!"<<endl; ?? ??? ?break; ?? ?}
?? ?::CloseHandle(h[0]); ?? ?::CloseHandle(h[1]); ?? ?cout<<"END"<<endl; ?? ?getchar(); ?? ?return 0; }
|