两个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/