Solved

setjmp/longjmp problem

Posted on 1997-05-30
4
285 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

943 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

10 Experts available now in Live!

Get 1:1 Help Now