sdyxidan的专栏

每天多学一点,管他学的是什么

互斥对象使用CreateMutex

msdn上对CreateMutex的说明是

CreateMutex function

Creates or opens a named or unnamed mutex object.

To specify an access mask for the object, use the CreateMutexEx function.

Syntax

HANDLE WINAPI CreateMutex(
  _In_opt_  LPSECURITY_ATTRIBUTES lpMutexAttributes,
  _In_      BOOL bInitialOwner,
  _In_opt_  LPCTSTR lpName
);

Parameters

lpMutexAttributes [in, optional]

A pointer to a SECURITY_ATTRIBUTES structure. If this parameter is NULL, the handle cannot be inherited by child processes.

The lpSecurityDescriptor member of the structure specifies a security descriptor for the new mutex. If lpMutexAttributes is NULL, the mutex gets a default security descriptor. The ACLs in the default security descriptor for a mutex come from the primary or impersonation token of the creator. For more information, see Synchronization Object Security and Access Rights.

bInitialOwner [in]

If this value is TRUE and the caller created the mutex, the calling thread obtains initial ownership of the mutex object. Otherwise, the calling thread does not obtain ownership of the mutex. To determine if the caller created the mutex, see the Return Values section.

lpName [in, optional]

The name of the mutex object. The name is limited to MAX_PATH characters. Name comparison is case sensitive.

If lpName matches the name of an existing named mutex object, this function requests the MUTEX_ALL_ACCESS access right. In this case, the bInitialOwner parameter is ignored because it has already been set by the creating process. If the lpMutexAttributesparameter is not NULL, it determines whether the handle can be inherited, but its security-descriptor member is ignored.

If lpName is NULL, the mutex object is created without a name.

If lpName matches the name of an existing event, semaphore, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returnsERROR_INVALID_HANDLE. This occurs because these objects share the same namespace.

The name can have a "Global\" or "Local\" prefix to explicitly create the object in the global or session namespace. The remainder of the name can contain any character except the backslash character (\). For more information, see Kernel Object Namespaces. Fast user switching is implemented using Terminal Services sessions. Kernel object names must follow the guidelines outlined for Terminal Services so that applications can support multiple users.

The object can be created in a private namespace. For more information, see Object Namespaces.

Return value

If the function succeeds, the return value is a handle to the newly created mutex object.

If the function fails, the return value is NULL. To get extended error information, callGetLastError.

If the mutex is a named mutex and the object existed before this function call, the return value is a handle to the existing object, GetLastError returns ERROR_ALREADY_EXISTSbInitialOwneris ignored, and the calling thread is not granted ownership. However, if the caller has limited access rights, the function will fail with ERROR_ACCESS_DENIED and the caller should use theOpenMutex function.

互斥对象CreateMutex(NULL,FALSE,"");第一个参数如果为NULL,表示子进程无法继承这个互斥对象,第二个参数为false表示互斥对象初始化后不会默认被创建进程占有,第三个参数表示互斥对象的名字。

互斥对象一旦被线程占有,则其他的线程无法再获取互斥对象而处于阻塞状态,只有占有线程通过ReleaseMutex释放互斥对象,其他线程才能够获取互斥对象,这样可以防止线程对数据的交叉访问而产生的错误。


#include <iostream>

#include <afx.h>
#include <process.h>
using namespace std;
HANDLE hUp;
CRITICAL_SECTION g_data;
int arr[10];
HANDLE hMutex; 
UINT __stdcall Add(LPVOID lParam)
{
    DWORD dReturn = WaitForSingleObject(hMutex,INFINITE);
    for (int i = 0; i<10;i++ )
    {
       arr[i]=i;//0-9
    }
    for ( i = 0;i < 10; i++)
    {
       cout<<arr[i]<<" ";
    }
    cout<<endl;
    ReleaseMutex(hMutex);
    return 1;
}
UINT __stdcall Add2(LPVOID lParam)
{
    DWORD dReturn = WaitForSingleObject(hMutex,INFINITE);
    for (int i = 0; i<10 ;i++)
    {
       arr [i] = i+100;//10`1
    }
    for ( i = 0;i < 10; i++)
    {
       cout<<arr[i]<<" ";
    }
    cout<<endl;
    ReleaseMutex(hMutex);
    return 1;
}
int main()
{
    hMutex = CreateMutex(NULL,FALSE,"");
    hUp=(HANDLE)_beginthreadex(NULL, 0, Add2, NULL, NULL, 0);
hUp=(HANDLE)_beginthreadex(NULL, 0, Add, NULL, NULL, 0);//线程创建之后是立即执行的,也不会阻塞。

    Sleep(5000);

}

output:


阅读更多
文章标签: c++ 互斥对象
个人分类: c
上一篇sizeof 和 strlen
下一篇内网穿透
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭