voidSubThread(void) { int i; for (i=0;i<5;i++) { cout << "SubThread" << i << endl; Sleep(2000);//改一下时间,输出顺序会有变化 } }
voidmain(void) { cout << "CreateThread" << endl;
// Create a thread; DWORD IDThread; HANDLE hThread; hThread = CreateThread(NULL, // no security attributes 0, // use default stack size (LPTHREAD_START_ROUTINE) SubThread, // thread function NULL, // no thread function argument 0, // use default creation flags &IDThread); // returns thread identifier // Check the return value for success. if (hThread == NULL) cout << "CreateThread error" << endl;
int i; for (i=0;i<5;i++) { cout << "MainThread" << i << endl; if (i==1) { if (SuspendThread(hThread)==0xFFFFFFFF) { cout << "Suspend thread error." << endl; } else { cout << "Suspend thread is ok." << endl; } } if (i==3) { if (ResumeThread(hThread)==0xFFFFFFFF) { cout << "Resume thread error." << endl; } else { cout << "Resume thread is ok." << endl; } }Sleep(4000);//改一下结果会有变化。 } }
参考:
MainThread0
SubThread0
SubThread1
MainThread1
SUspend thread is OK
MainThread2
MainThread3
Resume thread is OK
SubThread2
SubThread3
MainThread4
SubThread4