alarm against infinit loop

A

ankitks

Hi guys,
is there any utility available as a protection against endless_loop()

something like this:

alarm.set(5); //set timeout for 5 sec

endless_loop();

alarm.reset(); //reset it to 0, as alarm not needed any more

Thanks,
 
M

Mike Wahler

Hi guys,
is there any utility available as a protection against endless_loop()

something like this:

alarm.set(5); //set timeout for 5 sec

endless_loop();

alarm.reset(); //reset it to 0, as alarm not needed any more

Thanks,

Perhaps there exists such a utility, but as it
would not be standard C++, it would not be topical
here. However, a simple way to implement this would
involve instrumenting the loop itself. Before
the loop begins, capture a time with std::clock(),
and at one or more points inside the loop, capture
the time again. The difference between it and the
one obtained outside the loop would give elapsed time.
Compare it with your desired 'time-out' value. Break
the loop if elapsed time >= your time-out value.

See standard library documentation for details on
how to use 'std::clock()'.

-Mike
 
B

Binary

Hi guys,
is there any utility available as a protection against endless_loop()

something like this:

alarm.set(5); //set timeout for 5 sec

endless_loop();

alarm.reset(); //reset it to 0, as alarm not needed any more

pesudo code:

int flag = 0;

main(){
signal(SIGALRM, alarm_handler)
while (1) {
if (flag == 1)
break;
}
}

void alarm_handler()
{
flag = 1;
}

Actually, alarm signal will break the blocked system call.
 
I

Ian Collins

Hi guys,
is there any utility available as a protection against endless_loop()
Testing?

Anything else would be either intrusive, or platform specific
(signals/watchdog timers).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top