1

I thought that calling timed_receive() would just time out in this case, but instead it gets stuck attempting to lock a mutex.

So is there a function that I can call on my que, before attempting to receive data, which tells me if the sending process has died or is halted?

1 Answers1

0

There is no generic method to know whether the counter-party, died, halted, partially locked (eg one of the threads is in an infinite loop), playing possum, or just being adversarial. If the counter-party is cooperative, you can communicate with heartbeats and rely on them to decide if it responsive.

The fact that your program is "stuck" attempting to lock a mutex (are you sure that that's the case?) can indicate that,

  • the queue is empty, and the timeout value hasn't expired
  • you've run into a deadlock scenario.

The timed_receive succeeds, times out, or throws as expected:

bool timed_receive(void * buffer, std::size_t buffer_size, 
                   std::size_t & recvd_size, unsigned int & priority, 
                   const boost::posix_time::ptime & abs_time);

Receives a message from the message queue. The message is stored in buffer "buffer", which has size "buffer_size". The received message has size "recvd_size" and priority "priority". If the message queue is empty the receiver retries until time "abs_time" is reached. Returns true if the message has been successfully sent. Returns false if timeout is reached. Throws interprocess_error on error.

Also make sure you pass an absolute timeout value.

mockinterface
  • 14,452
  • 5
  • 28
  • 49
  • Okay, this may have just been a misunderstanding on my part. I had paused the sender in the debugger to test the timeout. I expected that the receiver would then time out and exit. But i think getting stuck in the shared mutex actually makes sense: the sender is still alive but not responding. If instead i kill the sender, the timeout does happen as expected. – David M. Cotter Feb 15 '14 at 18:38
  • @DavidM.Cotter Glad to hear! – mockinterface Feb 15 '14 at 22:24