Solved

setjmp/longjmp problem

Posted on 1997-05-30
4
280 Views
Last Modified: 2013-11-20
I know that the MSVC++ documentation says to use C++ try/catch instead of setjmp/longjmp, but I could not see how to get the same behavior.  So please, let's look at getting setjmp/longjmp first.

The problem is that the following piece of code fails in the
longjmp call.

   timeout_flag = FALSE;
   if ( setjmp( jbuf ) != 0 )
      timeout_flag = TRUE;
   else
   {
     Call SetTimer to execute a TimerProc in 10 seconds
   }
   if (!timeout_flag)
   {
     do some time consuming task
   }
   else
   {
     do something else
   }
   KillTimer to stop TimerProc for executing

In the TimerProc, timeout_flag is set to TRUE and
longjmp(jbuf,-1) is executed.  I do not understand
why it fails.  It must have something to do with
setting a timer function.

Why does this fail, and how can I get the TimerProc
to execute in 10 seconds (instead of when the system
happens to be free).  I have tried creating a thread
to keep track of the passage of time to get around
the message queue delay, but longjmp failed in the
same way.

The following piece of code runs without problems:

   timeout_flag = FALSE;
   if ( setjmp( jbuf ) != 0 )
      timeout_flag = TRUE;
   else
   {
      if (somethingHappens)
       longjump(jbuf,-1);
   }

   if (!timeout_flag)
  {
    do some time consuming task
  }
  else
  {
    do something else
  }

Anyone have a clue?
0
Comment
Question by:solomon021499
  • 3
4 Comments
 
LVL 3

Accepted Solution

by:
Melange earned 200 total points
Comment Utility
You're not supposed to be jumping across Window messages. For example a try/catch MUST be self-contained inside the handling of a message and I'm pretty sure the same thing holds true for setjmp/lonjmp. WM_TIMER messages (including timer procs) are NEVER guaranteed to be exactly accurate. They're almost always processed last in the queue like WM_PAINT messages.

If you had a timer of 10 seconds and the system was bogged down for 20 seconds, you would only receive one timer notification not 2. WM_TIMER only indicates that at least 10 seconds have passed.

What you could do is create a background thread to do the work you want and have it set a flag when finished or signal an event. The main thread can call GetTickCount to get the starting time before running the background thread and when idle or under timer messages call GetTickCount again to compare how many seconds have elapsed.
You'll also have to have a flag which the background thread checks in order to abort if necessary.

0
 

Author Comment

by:solomon021499
Comment Utility
I was afraid that this was the case.  You see, I am trying to port an X/Motif program to Windows using MSVC++/MFC and the program uses setjmp/longjmp.  The problem is that it uses alarm(unsigned int) to send a SIGALRM signal in a specified number of seconds and then the signal handler that handles the signal executes the longjmp.  Since there was no equivalent of the alarm() function on the Windows platform (that I know of) I tried the timer.  Please look at the piece of code below.  Is there really no other way to do this on Windows other that creating the other thread and setting state variables?  I had actually come up with the thread alternative, but I thought that there surely must be something better.

The original code looked like this:

flag = 0;
if ( setjmp( mark) != 0 )
  flag = 1;
else
{
  alarm( 5 );
  signal( SIGALRM, alrmHandler);
                // alrmHandler calls longjmp(mark,-1)
}
if ( !flag )
 start time consuming function
else
  do something else

alarm( 0 );
signal( SIGALRM, SIG_IGN );


If the time consuming function does not complete withing 5 seconds, the environment is set back, setjmp returns -1, and the else clause is executed.

Thanks.
0
 

Author Comment

by:solomon021499
Comment Utility
Melange, I meant to give you an A, but I wanted you to look at my comment.  I hit submit after I entered my comment, but I am not sure what grade was selected at the time.  If I did not give you an A, please send me an email at solomon@harborside.com and I will send an email to the appropriate party so that you get your quality points.
0
 

Author Comment

by:solomon021499
Comment Utility
Melange, I meant to give you an A, but I wanted you to look at my comment.  I hit submit after I entered my comment, but I am not sure what grade was selected at the time.  If I did not give you an A, please send me an email at solomon@harborside.com and I will send an email to the appropriate party so that you get your quality points.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now