Re: Implementing a Timeout in C/C++



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

wade.lindsey@xxxxxxxxx writes:

basically, a straitforward timeout...

Is there anyway to achieve this without using sleep? I have other
actions going on in the background that I don't want to suspend, I just
want a max-timed while loop - is that possible?? Trivial??

setitimer(2) or alarm(2) are good choices.

Install an SIGALRM signal handler, and use that to break out of the
loop by setting a condition variable of type sig_atomic_t.

Example:

http://cvs.alioth.debian.org/cgi-bin/cvsweb.cgi/schroot/schroot/sbuild-auth-conv-tty.h?rev=1.11&content-type=text/x-cvsweb-markup&cvsroot=buildd-tools
http://cvs.alioth.debian.org/cgi-bin/cvsweb.cgi/schroot/schroot/sbuild-auth-conv-tty.cc?rev=1.17&content-type=text/x-cvsweb-markup&cvsroot=buildd-tools

This is a password entry for TTYs, to hook into PAM. It uses a timer
to implement two timeouts: a warning timeout, and a fatal timeout.

In this case, there isn't a loop, it interrupts a read(2) system call,
but it could easily have been

while (!timer_expired)
{
...
}


Regards,
Roger

- --
Roger Leigh
Printing on GNU/Linux? http://gutenprint.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8+ <http://mailcrypt.sourceforge.net/>

iD8DBQFEGKsGVcFcaSW/uEgRAv4LAKDSXA1joJ4MU6ozUpWfQ7UrJWIGCwCgxv9e
SeXC1NiXUUFui8cEU9xwklw=
=Oj7c
-----END PGP SIGNATURE-----
.



Relevant Pages

  • Re: More accurate than sleep()
    ... > I once saw somewhere that there is a processor instruction to get the ... > precise than the sleep function to create a waiting period.. ... uses a sleep loop. ... timeout objects in timeout-time order and only times out the object with the ...
    (borland.public.delphi.language.basm)
  • Re: Password after standby interferes with SetForegroundWindow
    ... I'm at the mercy of a timeout I currently set to 10 minutes. ... Rather than have a loop as you currently have, ... to sleep again. ...
    (microsoft.public.win32.programmer.ui)
  • Re: Threading a server
    ... SMTP. ... needed the watchdog and with a reasonable amount of time. ... I am guessing that to deal with the timeout behaviour, ... The next message you posted commented on the loop. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Form OnKeypress event not firing while test loop running
    ... Paul E. Schoen wrote: ... Once this happens the display shows the running time and current value, and this may be repeated until a certain number of pulses occur, or a preset timeout is reached. ... The data analysis is accomplished in a rather large loop which fires on a 200 mSec timer event, and it processes about 480 integer data values. ... ESC key for example, then while your in that loop, you can test for that value incase the user wants to terminate the current function.. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Need Help in Loop Foreach
    ... I'd like to insert a loop, in such a way that it reads all the "System ID" ... # Query all the systems status by inputting the SystemIDs on Expect Script ... intial expect until a timeout occured. ... # may want to break after sending exit in above section if this does ...
    (comp.lang.tcl)