Windows同步技术-使用命名对象

在 Windows 系统下使用命名对象(如互斥体、事件、信号量、文件映射等内核对象)时,需注意以下关键要点:

命名规则

唯一性:名称需全局唯一,避免与其他应用或系统对象冲突,建议使用 GUID 或应用专属前缀(如 MyApp_SharedData)。

命名格式:名称可包含反斜杠 \,但避免使用特殊字符。若需跨会话或全局访问,需前缀 Global\ 或 Local\(默认作用域为会话内)。

作用域控制

会话内作用域:默认情况下,命名对象仅在当前用户会话内可见(隐式前缀为 Local\)。

全局作用域:使用 Global\ 前缀(如 Global\MyMutex)可使对象跨会话访问(常用于服务进程与用户进程通信)。

权限限制:访问全局对象可能需要管理员权限(Windows Vista+ 的安全机制)。

对象存在性检查

创建时检查:通过 Create* 函数(如 CreateMutex)的返回值及 GetLastError() 判断对象是否已存在(例如 ERROR_ALREADY_EXISTS)。

打开现有对象:使用 Open* 函数(如 OpenEvent)时,需确保对象已存在,否则返回 NULL。

原子性操作:多进程并发创建时需处理竞态条件。

安全性设置

安全描述符:通过 SECURITY_ATTRIBUTES 结构指定对象的访问权限,避免未经授权的进程访问。

继承性:若需子进程继承句柄,设置 bInheritHandle = TRUE。

资源管理

句柄关闭:使用 CloseHandle() 及时释放对象引用,避免资源泄漏。

生命周期:对象由内核管理,当所有句柄关闭后自动销毁(即使进程意外终止)。

示例代码

第一个进程使用 CreateMutex 函数创建互斥对象。 请注意,即使存在同名的现有对象,此函数也会成功。

复制代码
#include <windows.h>
#include <stdio.h>
#include <conio.h>

// This process creates the mutex object.

int main(void)
{
    HANDLE hMutex; 

    hMutex = CreateMutex( 
        NULL,                        // default security descriptor
        FALSE,                       // mutex not owned
        TEXT("NameOfMutexObject"));  // object name

    if (hMutex == NULL) 
        printf("CreateMutex error: %d\n", GetLastError() ); 
    else 
        if ( GetLastError() == ERROR_ALREADY_EXISTS ) 
            printf("CreateMutex opened an existing mutex\n"); 
        else printf("CreateMutex created a new mutex.\n");

    // Keep this process around until the second process is run
    _getch();

    CloseHandle(hMutex);

    return 0;
}

第二个进程使用 OpenMutex 函数打开现有互斥体的句柄。 如果不存在具有指定名称的互斥对象,则此函数将失败。 access 参数请求对互斥体对象的完全访问权限,这是在任何等待函数中使用的句柄所必需的。

复制代码
#include <windows.h>
#include <stdio.h>

// This process opens a handle to a mutex created by another process.

int main(void)
{
    HANDLE hMutex; 

    hMutex = OpenMutex( 
        MUTEX_ALL_ACCESS,            // request full access
        FALSE,                       // handle not inheritable
        TEXT("NameOfMutexObject"));  // object name

    if (hMutex == NULL) 
        printf("OpenMutex error: %d\n", GetLastError() );
    else printf("OpenMutex successfully opened the mutex.\n");

    CloseHandle(hMutex);

    return 0;
}
相关推荐
路由侠内网穿透1 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
研华嵌入式2 天前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
带娃的IT创业者2 天前
Windows 平台上基于 MCP 构建“文心一言+彩云天气”服务实战
人工智能·windows·文心一言·mcp
csdn_aspnet2 天前
Windows Node.js 安装及环境配置详细教程
windows·node.js
摇滚侠2 天前
java语言中,list<String>转成字符串,逗号分割;List<Integer>转字符串,逗号分割
java·windows·list
Source.Liu2 天前
【Pywinauto库】12.2 pywinauto.element_info 后端内部实施模块
windows·python·自动化
Source.Liu2 天前
【Pywinauto库】12.1 pywinauto.backend 后端内部实施模块
开发语言·windows·python·自动化
私人珍藏库2 天前
[Windows] FileOptimizer v17.1.0_一款文件批量压缩工具
windows·批量压缩
掘根2 天前
【CMake】List
windows·microsoft·list
TToolss2 天前
删除文件夹里的网盘图标
windows