[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

setjmp/longjmp problem

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
solomon021499
Asked:
solomon021499
  • 3
1 Solution
 
MelangeCommented:
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
 
solomon021499Author Commented:
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
 
solomon021499Author Commented:
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
 
solomon021499Author Commented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now