Using signals to unblock calls to msgrcv() in a multi-threaded process (Cygwin v1.7.33)

Corinna Vinschen corinna-cygwin@cygwin.com
Mon Jan 19 11:28:00 GMT 2015


On Jan 19 08:29, SCOTT Damien wrote:
> I have a multi-threaded process running under Cygwin that receives
> messages from several IPC message queues (built using Cygwin's gcc,
> version 4.8.3).  The listener for each queue runs in its own thread,
> each of which is created from the main thread.  Each listener is using
> a blocking call to msgrcv().  I want the main thread to be able to use
> pthread_kill() to send a SIGUSR1 signal to each listening thread to
> cause it to abandon its call to msgrcv() and then exit.  
> 
> My approach has been to create a signal mask containing just SIGUSR1
> and associate this with a signal handler (although the signal handler
> contains no functionality, since all I need is for the blocking call
> to msgrcv() to be abandoned).  I initially set the signal mask to
> block in the main thread, which should then be inherited by each of
> the created listening threads.  I only unblock the signal mask around
> the call to msgrcv() in each listening thread.
> 
> I have included a simplified version of my code below.  I usually
> don't see any evidence of the SIGUSR1 signal being received by either
> of the listener threads.  Occasionally the first thread unblocks as
> expected but I have never seen both threads unblock.  If I change my
> code so that there is only a single listener thread, then everything
> works correctly every time.  Changing the msgrcv() call to a sleep
> call also allows unblocking to occur correctly.  Note that this
> simplified code does not include removal of the created queues (since
> it never manages to exit anyway) so I manually remove these after each
> run using the ipcrm command.
> 
> Example output:
> 
> Running thread 2 with queue Id = 1441792
> Running thread 1 with queue Id = 1179649
> About to kill thread 1
> 
> Is there an issue with the Cygwin implementation of signal handling,
> or is there a problem with my code?  Any help would be much
> appreciated.

It's an issue with the Cygwin code, probably.  The msgrcv code ultimately
hangs in a blocking ReadFile call on a named pipe, and this call is
non-interruptible.  Changing that requires a bigger change in Cygwin
which will take time.

For the time being, would it be much of a problem for you to convert
your stuff to POSIX message queues(*)?  These don't require running
cygserver, and they are supposed not to suffer from this problem.
Fixing them in case of a bug is much easier as well.


Corinna

(*) http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/mqueue.h.html

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://cygwin.com/pipermail/cygwin/attachments/20150119/1f2dd9f6/attachment.sig>


More information about the Cygwin mailing list