windows下信号量和共享内存api, mark:共享内存实现进程间锁

关于信号量的:
HANDLE hSem = OpenSemaphore( SEMAPHORE_ALL_ACCESS ,FALSE, "my_mutex");
if (!hSem)
{
hSem = CreateSemaphore(NULL, 1, 1, "my_mutex");
if (hSem == INVALID_HANDLE_VALUE)
{
return false;
}
}

DWORD ret = WaitForSingleObject(hSem, 5000);
if (ret == WAIT_OBJECT_0)
{
Sleep(100000);
}

ReleaseSemaphore(hSem, 1, NULL);
CloseHandle(hSem);


关于共享内存的:
HANDLEhandle;

handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, TRUE, "test");
if (handle == NULL)
{
handle = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 256, "test");
if (handle == NULL)
{
return 1;
}
}

char *p =reinterpret_cast(MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, 0));
if (!p)
{
UnmapViewOfFile(p);
CloseHandle(handle);
return 1;
}

UnmapViewOfFile(p);
CloseHandle(handle);



【windows下信号量和共享内存api, mark:共享内存实现进程间锁】另外: 看到一种做法是用共享内存中的实现进程间锁的方式,这个没有完全确定共享内存的数据读写一定不经过cpu缓冲,或者是经过了且满足cpu缓存一致性原则。所以没有确认共享内存实现的进程间锁一定没有问题。 找个时间确认一下这个问题吧,mark~








    推荐阅读