winapi - 互锁*功能对共享内存有用吗?

标签 winapi ipc shared-memory interlocked

两个Windows进程的内存映射了相同的共享文件。如果文件包含计数器,是否适合使用Interlocked*函数(例如InterlockedIncrement)来更新这些计数器?那些会同步跨进程的访问吗?还是我需要使用更重的东西,例如互斥锁?也许共享内存机制本身可以确保一致的 View 。

最佳答案

MSDN:

...

The Interlocked API

The interlocked functions provide a simple mechanism for synchronizing access to a variable that is shared by multiple threads. They also perform operations on variables in an atomic manner. The threads of different processes can use these functions if the variable is in shared memory.



因此,是的,使用共享内存方法是安全的。

关于winapi - 互锁*功能对共享内存有用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1637725/

相关文章:

c++ - 在共享内存上使用 volatile 安全吗?

windows - Win32 CreateProcess : When is CREATE_UNICODE_ENVIRONMENT *really* needed?

c - 我的程序一直在等待,甚至不显示第一个 printf

c - c 中的数据报套接字。看起来父进程从不绑定(bind)

c - 共享内存中的指针 - C 语言 Linux

c - 共享变量只在一个进程中改变

c++ - 麦克风如何录音到没有声音?

delphi - 最好的报告编写工具是什么?

windows - (Windows)系统文件缓存磁盘回写失败会出现什么错误?他们是如何被报道的?

asp.net - 在 IIS 中部署时打开内存映射文件会导致 FileNotFoundException