多线程之互斥锁(By C++)

  首先贴一段win32API实现的多线程的代码,使用CreateThread实现,若是不要传参数,就把第四个参数设为NULLios

  

#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;
}

下面是执行的结果多线程

 

互斥锁:spa

  当一个全局的共有资源被多个线程同时调用会出现意想不到的问题,好比你去银行取出全部钱,同时又转全部钱到支付宝,若是这两块同时执行,就有可能转出双倍的钱,这是不容许的。线程

这时候要使用的这个线程须要将这个资源(取钱这个过程)先“锁”起来,而后用好以后再解锁,这期间别的线程就没法使用了,其余线程的也是相似的过程。code

#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会交替执行blog

多进程互斥:进程

若是某个文件不容许被多个进程用时使用,这时候也能够采用进程间互斥。当一个进程建立一个进程后建立一个锁,第二个进程使用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;
}

第二个进程:string

#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;
}

结果呢就是,两个线程执行打印一次就死掉了

相关文章
相关标签/搜索