From: Peter Petrov (ppetrov_at_[hidden])
Date: 2005-09-23 19:52:48


Jason Hise wrote:

> Alright, I will add some more context to the discussion. This is being
> used for a 'timeout lifetime' for the singleton library. The idea is
> that a singleton which is cheap to create but expensive to hold on to
> could automatically be destroyed if it goes unused a certain amount of
> time, and be recreated the next time it is needed. To implement this
> idea I create a timeout thread when the instance is created, and force
> that thread to sleep until the timeout time is reached. If at that
> point the timeout time has been extended, the thread sleeps again,
> otherwise it destroys the instance and exits.
>
> The problem is that if the process is exiting I would like to force the
> thread to wake up and time out early to ensure that the singleton get
> destroyed.

Instead of sleeping, use timed_wait on a condition variable. Upon
awakening, check the cause (which could be timeout, valid signal, or
spurious wakeup).