[QUERY] Help With Semaphore Error on Windows : Cygwin
Corinna Vinschen
corinna-cygwin@cygwin.com
Thu Feb 16 14:59:36 GMT 2023
Hi Kai,
Apart from what Takashi already wrote, there's another bug in this code:
On Feb 16 12:04, Yeo Kai Wei via Cygwin wrote:
> #define ByteSize 512
> #define BackingFile "/shMemEx"
> #define AccessPerms 0644
> #define SemaphoreName "mysemaphore"
^^^^^^^^^^^^
What Takashi wrote. The reason that you don't notice that sem_open
actuially failed is...
> //Create the semaphore
> sem_t* semptr = sem_open( SemaphoreName,//name
> O_CREAT, //create semaphore
> AccessPerms, //protection permissions
> 0); //Initial value
>
> //ERROR
> if(semptr == (void*) -1)
^^^^^^^^^^
This.
Why do you test for -1? If you read the POSIX man page for sem_open,
you'll see this:
Upon successful completion, the sem_open() function shall return the
address of the semaphore. Otherwise, it shall return a value of
SEM_FAILED [...]
^^^^^^^^^^
SEM_FAILED is not necessarily -1. On Cygwin it's defined as
#define SEM_FAILED ((sem_t *) 0)
in /usr/include/semaphore.h.
So your code just seems to fail in sem_post, but actually that's
because sem_open failed and your code checks for the wrong return
value.
HTH,
Corinna
More information about the Cygwin
mailing list