VS2010 多线程编程
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; }