Solved

setjmp/longjmp problem

Posted on 1997-05-30
4
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

726 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