/* longjmp example */ #include #include #include #include #define RETURN_NOTHING return; #define ABS_ZERO (1-1) #define BOOLEAN int #define MAX_LOOPS 15000 struct salesinfo { int count; int values[MAX_LOOPS+1]; }; int subtotal; void addvaluetosubtotal(int in_value) { subtotal += in_value; } /* this subroutine is called thousands of times. use longjmp instead of loops to increase speed. Daren 12/03/05 */ void calculate(struct salesinfo*sales){ jmp_buf buffer; int i=setjmp(buffer); if (!(icount)) RETURN_NOTHING; addvaluetosubtotal(sales->values[i]); if (icount){ longjmp(buffer,i+1); } } main() { int t, m; struct salesinfo s; jmp_buf my_buffer, my_buffer2; time_t timer; t = setjmp(my_buffer2); if (t < MAX_LOOPS) { s.values[t] = t; s.count = t+1; longjmp(my_buffer2,t+1); } timer=time(NULL); printf("Start:%s\n",asctime(localtime(&timer))); m = setjmp(my_buffer); if (m < 5000) { /* function comment says calculate is called "thousands of times" 5000 should be enough! */ subtotal = 0; calculate(&s); longjmp(my_buffer,m+1); } timer=time(NULL); printf(" End:%s\n\n",asctime(localtime(&timer))); printf("Press Enter to Continue"); getch(); return 0; }