0

What is the best and shortest way to create a WatchDogTimer class, which can wait some time and then died?

So when I'm doing something like

WatchDogTimer wdt (10);

wdt will be available only in next 10 seconds.

How can I do this?

ps: yeah, I saw that thread, but it did not actually help.

Community
  • 1
  • 1
  • You can't. When you allocate something that way, you're allocating it on the stack. The only thing that will remove it from the stack is exiting the scope (i.e. returning from the method) containing that variable. In other words, nothing you can control in terms of time. You COULD do it using threads or signals, but the object itself would need to be allocated on the heap (i.e. use the `new` operator). Please give us an example of WHY you want to do this, it may help us give more intelligent answers. – Chris Eberle Apr 05 '11 at 04:03

1 Answers1

1

Create an object on the heap with new and hand it to a shared_ptr. Then hold that shared pointer only from a thread that sleeps for 10 seconds before exiting. Make sure the thread is the only instance of shared_ptr and all other references are weak_ptrs. After the 10 seconds weak_ptr::get() will return null.

Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94