多线程之互斥锁(By C++)
首先贴一段win32API实现的多线程的代码,使用CreateThread实现,如果不要传参数,就把第四个参数设为NULL
#include<Windows.h> #include<iostream> using namespace std; //有参数 DWORD WINAPI MyThread_lpParamter(LPVOID lpParamter) { string *lp = (string *)lpParamter; while (1) { cout << "MyThread1 Runing :"<<lp->c_str()<<""<< endl; Sleep(5000); } } int main() { string parameter = "我是参数"; HANDLE hThread2 = CreateThread(NULL, 0, MyThread_lpParamter, ¶meter, 0, NULL); CloseHandle(hThread2); while(1); return 0; }
下面是执行的结果
互斥锁:
当一个全局的共有资源被多个线程同时调用会出现意想不到的问题,比如你去银行取出所有钱,同时又转所有钱到支付宝,如果这两块同时执行,就有可能转出双倍的钱,这是不允许的。
这时候要使用的这个线程需要将这个资源(取钱这个过程)先“锁”起来,然后用好之后再解锁,这期间别的线程就无法使用了,其他线程的也是类似的过程。
#include<Windows.h> #include<iostream> using namespace std; //互斥锁 HANDLE hMutex1; int flag; DWORD WINAPI MyThread2(LPVOID lpParamter) { while (1) {
//没上锁的话就自己锁上,否则等着 WaitForSingleObject(hMutex1,INFINITE); flag=!flag; cout << "MyThread1 Runing :"<<"线程2"<<" "<<flag<< endl; Sleep(1000);
//解锁 ReleaseMutex(hMutex1); } } DWORD WINAPI MyThread1(LPVOID lpParamter) { while (1) { WaitForSingleObject(hMutex1,INFINITE); flag=!flag; cout << "MyThread2 Runing"<<"线程1" <<" "<<flag<< endl; Sleep(10); ReleaseMutex(hMutex1); } } int main() { //创建一个锁 hMutex1 =CreateMutex(NULL,FALSE,NULL); HANDLE hThread1 = CreateThread(NULL, 0, MyThread1, NULL, 0, NULL); CloseHandle(hThread1); HANDLE hThread2 = CreateThread(NULL, 0, MyThread2, NULL, 0, NULL); CloseHandle(hThread2); while(1); return 0; }
可以看到结果,就算线程1延时的时间非常短,但是由于线程2执行的时候,就被锁住了,线程1就处于等待。结果就是线程1和线程2会交替执行
多进程互斥:
如果某个文件不允许被多个进程用时使用,这时候也可以采用进程间互斥。当一个进程创建一个进程后创建一个锁,第二个进程使用OpenMutex获取第一个进程创建的互斥锁的句柄。
第一个进程:
#include<Windows.h> #include<iostream> using namespace std; //互斥锁 HANDLE hMutex1; int flag; DWORD WINAPI MyThread(LPVOID lpParamter) { while (1) { WaitForSingleObject(hMutex1,INFINITE); flag=!flag; cout << "MyThread2 Runing"<<"进程1" <<" "<<flag<< endl; Sleep(500); //此时锁1被锁,无法在下面解锁2 ReleaseMutex(hMutex1); } } int main() { //创建一个锁 hMutex1 =CreateMutex(NULL,false,LPCWSTR("hMutex1")); HANDLE hThread1 = CreateThread(NULL, 0, MyThread, NULL, 0, NULL); CloseHandle(hThread1); while(1); return 0; }
第二个进程:
#include<Windows.h> #include<iostream> using namespace std; //互斥锁 HANDLE hMutex1; int flag; //无参数 DWORD WINAPI MyThread(LPVOID lpParamter) { while (1) { WaitForSingleObject(hMutex1,INFINITE); flag=!flag; cout << "MyThread2 Runing"<<"进程2" <<" "<<flag<< endl; Sleep(5000); ReleaseMutex(hMutex1); } } int main() { //打开 hMutex1 = OpenMutex(MUTEX_ALL_ACCESS,false,LPCWSTR("hMutex1")); if(hMutex1!=NULL) cout<<"锁打开成功"<<endl; HANDLE hThread1 = CreateThread(NULL, 0, MyThread, NULL, 0, NULL); CloseHandle(hThread1); while(1); return 0; }
结果可以看到,之运行进程1,消息打印的非常快,但是把进程2打开之后,进程1的消息打印速度就跟进程2变得一样了。
死锁:
何为死锁,举个例子,两个柜子,两个锁,两把钥匙,把两把钥匙放进另外一个柜子,然后锁上,结果呢,两个都打不开了。在程序内部,这样就会导致两个进程死掉。
看例子
#include<Windows.h> #include<iostream> using namespace std; //互斥锁 HANDLE hMutex1; HANDLE hMutex2; int flag; DWORD WINAPI MyThread2(LPVOID lpParamter) { while (1) { WaitForSingleObject(hMutex1,INFINITE); flag=!flag; cout << "MyThread1 Runing :"<<"线程1"<<" "<<flag<< endl; Sleep(1000); //此时锁2被锁,无法在下面解锁1 WaitForSingleObject(hMutex2,INFINITE); ReleaseMutex(hMutex2); ReleaseMutex(hMutex1); } } DWORD WINAPI MyThread1(LPVOID lpParamter) { while (1) { WaitForSingleObject(hMutex2,INFINITE); flag=!flag; cout << "MyThread2 Runing"<<"线程1" <<" "<<flag<< endl; Sleep(1000); //此时锁1被锁,无法在下面解锁2 WaitForSingleObject(hMutex1,INFINITE); ReleaseMutex(hMutex1); ReleaseMutex(hMutex2); } } int main() { //创建一个锁 hMutex1 =CreateMutex(NULL,FALSE,NULL); hMutex2 =CreateMutex(NULL,FALSE,NULL); HANDLE hThread1 = CreateThread(NULL, 0, MyThread1, NULL, 0, NULL); CloseHandle(hThread1); HANDLE hThread2 = CreateThread(NULL, 0, MyThread2,NULL, 0, NULL); CloseHandle(hThread2); while(1); return 0; }
结果呢就是,两个线程执行打印一次就死掉了