Solved

setjmp/longjmp problem

Posted on 1997-05-30
4
286 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
ID: 1301900
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
ID: 1301901
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
ID: 1301902
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
ID: 1301903
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

808 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