design Q : using timer/threads

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Joan Coleman (JoanColeman_at_discussions.microsoft.com)
Date: 10/21/04


Date: Thu, 21 Oct 2004 12:41:54 -0700

Hi All

I have not used multimedia timers nor threads in an MFC program as yet. I
have searched through the newsgroups and done a lot of reading (incl. MVP
sites - which are very helpful, thanks) and have come up with a design for my
application. I am including the pseudo code and would really appreciate any
suggestions regarding whether it will work. I do not want to start coding and
find out I got something really stupid in here. If this is too much to ask,
then perhaps some pointers as to what might be done better. TIA for all help.

The task ( a "real time" data acquisition system) is as follows: at some
point, the program (dialog) goes into a loop waiting on the user to interact
via FKEYS; when the user enters FKEY1 the data collection process starts up;
another FKEY1 press stops the data collection. (this can occur a lot of
times). When the user is completely finished, via FKEY 8,the data acquisition
part of the program is over .When data collection is not active, the user can
interact via other function keys. When data collection is active, the user
cannot interact, except by depressing FKEY 1 and by triggering an external
event button.

I am using VC 6.0 on a Windows XP Pro system - 3 GHz machine with dual
processors. The program is dialog based; the dialog which is running this
piece of code is a modal dialog started from the main dialog.

Data Collection code:

loop1 = loop2 = true;
Start worker thread 1; //polls a network for tracking data and stores in
dialog vars
while (loop1)
     while(loop2)
          FKEY1 //start run
               setup multimedia timer (1 millisec) with callback function
               loop2 = false;
               endrun = false;
         FKEY2:
         FKEY3:
         .
         .
         FKEY8: //end DAQ
               loop2 = false;
               loop1 = false;
               stop worker thread 1;
          end loop2;
           update the dialog display; (via control variables)
           if endrun
                stop multimedia timer;
                loop2 = true;
          end if
end loop1;

 the CALLBACK handler:

CallBack
    fetch some instrument readings;
    build record containing A/D, tracking info, etc. and move to buffer
   check on event input;
    if FKEY 1 pressed
         write final buffer
         endrun = true
    else
         if buffer full
               start thread to write to disk;
               update buffering controls;
         end if
     end if
end callback
  
A couple assumptions/questions:
endrun needs to be volatile
the tracking data needs some sort of lock/unlock control governing its access.
A specific question: how should other variables be declared so that the
threads/callback procedure may access them - as private vars in the header
section of the class?????
Also, can I write to the disk (the file is a CFile object) in a thread, or
should it be moved back in the loop1 code? (think I read somewwhere not to
use MFC stuff in threads).

Believe I also read somewhere that a 2 ms interrupt won't impact the system
overhead too badly, but that 1ms will. Would it be better to put the
callback code into a loop to do it twice, and use a 2 ms interrupt?

   
          


Quantcast