From 23800d6cbb3f516a6ba8122cc8f85384869a9a99 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Sat, 28 Nov 2015 16:06:26 +0100 Subject: [PATCH] Fixed shared memory issues under Windows, by Erik Zandvliet. --- lib/shared_memory.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/shared_memory.cpp b/lib/shared_memory.cpp index 8b678be4..fb0189df 100644 --- a/lib/shared_memory.cpp +++ b/lib/shared_memory.cpp @@ -123,7 +123,22 @@ namespace IPC { int timer = 0; while (!(*this) && timer++ < 10){ #if defined(__CYGWIN__) || defined(_WIN32) - mySem = CreateSemaphore(0, value, 1 , std::string("Global\\" + std::string(name)).c_str()); + std::string semaName = "Global\\"; + semaName += name; + if (oflag & O_CREAT){ + if (oflag & O_EXCL){ + //attempt opening, if succes, close handle and return false; + HANDLE tmpSem = OpenSemaphore(0, false, semaName.c_str()); + if (tmpSem){ + CloseHandle(tmpSem); + mySem = 0; + break; + } + } + mySem = CreateSemaphore(0, value, 1 , semaName.c_str()); + }else{ + mySem = OpenSemaphore(0, false, semaName.c_str()); + } #else if (oflag & O_CREAT) { mySem = sem_open(name, oflag, mode, value);