1、多線程測試實例:
此時在多核CPU下,主線程和子線程可同時運行;時間片是怎么分配的呢?感覺好亂呢?
#include <windows.h> #include <iostream> #include <stdlib.h> using namespace std; DWORD WINAPI MyThreadProc1(LPVOID lpParameter); DWORD WINAPI MyThreadProc2(LPVOID lpParameter); int index = 0; int i = 0 , y = 0; int main() { HANDLE handle1,handle2; handle1 = CreateThread(NULL,0,MyThreadProc1,NULL,0,NULL); handle2 = CreateThread(NULL,0,MyThreadProc2,NULL,0,NULL); if(NULL == handle1) { cout<<"Create Thread failed !"<<endl; return -1; } if(NULL == handle2) { cout<<"Create Thread failed !"<<endl; return -1; } CloseHandle(handle1); CloseHandle(handle2); cout<<"The Main Thread is Running !"<<endl; system("PAUSE"); return 0; } DWORD WINAPI MyThreadProc1(LPVOID lpParameter) { cout<<"The MyThreadProc1 is Running !"<<endl; return 0; } DWORD WINAPI MyThreadProc2(LPVOID lpParameter) { cout<<"The MyThreadProc2 is Running ! "<<endl; return 0; }
2、線程同步:
使用互斥對象實現線程同步,主要用到的函數:
CreateMutex(); 其第二個參數若設為TRUE,則主線程擁有一個CPU內核;創建帶名字的互斥對象,根據第三個參數可判斷互斥對象已存在;
ReleaseMutex(); 若線程運行完成后,會自動釋放互斥對象;
WaitForSingleObject();
(1)沒有同步的實例:
#include <windows.h> #include <iostream> #include <stdlib.h> using namespace std; DWORD WINAPI MyThreadProc1(LPVOID lpParameter); DWORD WINAPI MyThreadProc2(LPVOID lpParameter); int index = 100; int main() { HANDLE handle1,handle2; handle1 = CreateThread(NULL,0,MyThreadProc1,NULL,0,NULL); handle2 = CreateThread(NULL,0,MyThreadProc2,NULL,0,NULL); if(NULL == handle1) { cout<<"Create Thread failed !"<<endl; return -1; } if(NULL == handle2) { cout<<"Create Thread failed !"<<endl; return -1; } CloseHandle(handle1); CloseHandle(handle2); Sleep(2000); system("PAUSE"); return 0; } DWORD WINAPI MyThreadProc1(LPVOID lpParameter) { while(true) { if(index > 0) { Sleep(1); cout<<"The Index1 Number is : "<<index--<<endl; } else break; } return 0; } DWORD WINAPI MyThreadProc2(LPVOID lpParameter) { while(true) { if(index > 0) { Sleep(1); cout<<"The Index2 Number is : "<<index--<<endl; } else break; } return 0; }
(2)互斥對象實現多線程同步:根據個人電腦CPU而異,此代碼中main()占有一個CPU內核;且沒有使用ReleaseMutex()釋放所占CPU;
#include <windows.h> #include <iostream> #include <stdlib.h> using namespace std; DWORD WINAPI MyThreadProc1(LPVOID lpParameter); DWORD WINAPI MyThreadProc2(LPVOID lpParameter); DWORD WINAPI MyThreadProc3(LPVOID lpParameter); int index = 100; HANDLE hMutex; int main() { HANDLE handle1,handle2,handle3; handle1 = CreateThread(NULL,0,MyThreadProc1,NULL,0,NULL); handle2 = CreateThread(NULL,0,MyThreadProc2,NULL,0,NULL); handle3 = CreateThread(NULL,0,MyThreadProc3,NULL,0,NULL); if(NULL == handle1) { cout<<"Create Thread failed !"<<endl; return -1; } if(NULL == handle2) { cout<<"Create Thread failed !"<<endl; return -1; } if(NULL == handle3) { cout<<"Create Thread failed !"<<endl; return -1; } CloseHandle(handle1); CloseHandle(handle2); CloseHandle(handle3); hMutex = CreateMutex(NULL,TRUE,NULL); Sleep(2000); system("PAUSE"); return 0; } DWORD WINAPI MyThreadProc1(LPVOID lpParameter) { while(true) { WaitForSingleObject(hMutex,INFINITE); if(index > 0) { Sleep(1); cout<<"The Index1 Number is : "<<index--<<endl; } else break; } return 0; } DWORD WINAPI MyThreadProc2(LPVOID lpParameter) { while(true) { WaitForSingleObject(hMutex,INFINITE); if(index > 0) { Sleep(1); cout<<"The Index2 Number is : "<<index--<<endl; } else break; } return 0; } DWORD WINAPI MyThreadProc3(LPVOID lpParameter) { while(true) { WaitForSingleObject(hMutex,INFINITE); if(index > 0) { Sleep(1); cout<<"The Index3 Number is : "<<index--<<endl; } else break; } return 0; }