hMutex


#include <iostream>
#include <Windows.h>
#include <tchar.h>

using namespace std;
HANDLE hMutex;

//HANDLE CreateMutex(LPSECURITY_ATTRIBUTES lpMutexAttributes,
            //BOOL bInitialOwner,
            //LPCTSTR lpName);          
// bInitialOwner:指定该资源初始时是否归属创建它的进程
// lpName:指定互斥量资源的名称

// BOOL ReleaseMutex( __in HANDLE hMutex );
// 用于释放一个独占资源,进程一旦释放该资源,该资源就不在属于它了,
// 如果还要用,需要重新申请得到该资源,申请资源函数
// DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds);


DWORD WINAPI Fun(LPVOID pParam)
{
    while(1)
    {
        WaitForSingleObject(hMutex, INFINITE);
        cout << "Fun display!" << endl;
        Sleep(100);
        ReleaseMutex(hMutex);
    }
}


int main()
{
    HANDLE hThread = CreateThread(NULL, 0, Fun, NULL, 0, NULL);
    // 创建一个名为screen的互斥量,并且初始时这个互斥量不属于创建
    // 它的这个进程
    hMutex = CreateMutex(NULL, FALSE, _T("screen"));
    CloseHandle(hThread);

    while(1)
    {
        WaitForSingleObject(hMutex, INFINITE);
        cout << "main display!" << endl;
        Sleep(200);
        ReleaseMutex(hMutex);
    }

    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值