In article <ce********@library1.airnews.net>,
Gordon Burditt <go***********@burditt.org> wrote:
Can anyone explain me why do we use setjmp and longjump functions.
I read through the manual pages/doc but wasnt able to get a clear
picture of the same.
setjmp and longjmp are a long-distance version of goto, and
therefore they are greatly frowned upon by the Structured Programming Police.
setjmp can also behave as a COME FROM, which is occasionally useful,
especially if something like this needs to be added to an existing
program:
--------
static jmp_buf env;
void go_back_to_sleep(void)
{
longjmp(env,1);
}
int main(void)
{
/*Do one-time initialization*/
/*This means "COME FROM longjmp in go_back_to_sleep()"*/
setjmp(env);
/*Wait for wake-up call*/
/*Do per-run initialization*/
while(1)
{
/*Do stuff, including calling functions that may call
go_back_to_sleep() under appropriate conditions
*/
}
/*never reached*/
}
--------
This is probably not the best way to do this in new code, but it's
often the easiest way to change an existing program's handling of "We're
done doing stuff" from "exit" to "go back to sleep and wait for another
wake-up call".
dave
--
Dave Vandervies
dj******@csclub.uwaterloo.ca
It's like trying to give yourself a voluntary lobotomy.
--Andy Glew in comp.arch