天天看點

多線程之互斥鎖(By C++)

首先貼一段win32API實作的多線程的代碼,使用CreateThread實作,如果不要傳參數,就把第四個參數設為NULL

  

多線程之互斥鎖(By C++)
#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, &parameter, 0, NULL);
    CloseHandle(hThread2);
    while(1);
    return 0;
}      
多線程之互斥鎖(By C++)

下面是執行的結果

多線程之互斥鎖(By C++)

互斥鎖:

  當一個全局的共有資源被多個線程同時調用會出現意想不到的問題,比如你去銀行取出所有錢,同時又轉所有錢到支付寶,如果這兩塊同時執行,就有可能轉出雙倍的錢,這是不允許的。

這時候要使用的這個線程需要将這個資源(取錢這個過程)先“鎖”起來,然後用好之後再解鎖,這期間别的線程就無法使用了,其他線程的也是類似的過程。

多線程之互斥鎖(By C++)
#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;
}

      
多線程之互斥鎖(By C++)

可以看到結果,就算線程1延時的時間非常短,但是由于線程2執行的時候,就被鎖住了,線程1就處于等待。結果就是線程1和線程2會交替執行

多線程之互斥鎖(By C++)

多程序互斥:

如果某個檔案不允許被多個程序用時使用,這時候也可以采用程序間互斥。當一個程序建立一個程序後建立一個鎖,第二個程序使用OpenMutex擷取第一個程序建立的互斥鎖的句柄。

第一個程序:

多線程之互斥鎖(By C++)
#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;
}      
多線程之互斥鎖(By C++)

第二個程序:

多線程之互斥鎖(By C++)
#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;
}      
多線程之互斥鎖(By C++)

結果可以看到,之運作程序1,消息列印的非常快,但是把程序2打開之後,程序1的消息列印速度就跟程序2變得一樣了。

死鎖:

何為死鎖,舉個例子,兩個櫃子,兩個鎖,兩把鑰匙,把兩把鑰匙放進另外一個櫃子,然後鎖上,結果呢,兩個都打不開了。在程式内部,這樣就會導緻兩個程序死掉。

看例子

多線程之互斥鎖(By C++)
#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;
}      
多線程之互斥鎖(By C++)

結果呢就是,兩個線程執行列印一次就死掉了

多線程之互斥鎖(By C++)
c++

繼續閱讀