home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.os.msdos.programmer
- Path: sparky!uunet!gator!miles!sjm
- From: sjm@miles.com (Scott Mark)
- Subject: Re: Borland C++'s printf
- Message-ID: <1993Jan26.102143.24555@miles.com>
- Organization: Miles Inc., Diagnostics Division, Elkhart, IN
- References: <1jvp5jINNe3q@uniwa.uwa.edu.au>
- Date: Tue, 26 Jan 1993 10:21:43 GMT
- Lines: 51
-
- In article <1jvp5jINNe3q@uniwa.uwa.edu.au> rmascaro@uniwa.uwa.edu.au (Robert Mascaro) writes:
- >I am not sure that this is the right group but here goes.
- >I am writing a TSR using C++ (borland's) and are having trouble with the
- >printf statement. I am outputing integer variables to the screen and
- >need a printf("%i",x) etc statement. The problem is that it crashes at
- >the point of output each time. I have tryed a "cputs" to test the
- >problem and this works fine, however any printf statement will crash the
- >machine.
- >As background, the TSR I mentioned makes a clock tick on the screen
- >while using DOS.
- >Any suggestions, advice??
- >Thanks in advance,
- >Rob.
-
- This may help to explain the problem- it may not.
-
- When you try using things like printf() in a TSR you run the risk of
- interfering with another "thread" of the function that is running in
- the foreground.
-
- There are two ways of preventing disaster in this case. The simplest way
- is to have a semaphore that all the threads fight over. A semaphore works
- the same way as a railroad semaphore -- the guy with the green light gets
- to go and everyone else waits to use the track.
-
- When you use a semaphore, you are free to declare local static data
- foo()
- {
- static char data[36];
- }
- because only your thread will be writing into that area.
-
- A slightly more complicated way to do this is to write the functions to
- be "re-entrant", which means that there is no static local data used by
- the function (or any functions that it calls). With a re-entrant function,
- each thread has its own copy of the data on the stack, so the threads don't
- interfere with each other's variables.
-
- If memory serves, the printf() function for Microsoft C6 was written to
- be re-entrant. I wouldn't bet your purchase price for C7 on it, though.
- It's been a long time since I've been in contact with Peter Golde, who
- wrote the thing.
-
- It's common for routines like printf() to either use static data areas or
- to call functions that do. This may be what is happening to your TSR.
-
- Have you tried using sprintf() and then cputs()? You may have to write
- your own routine to do this job.
-
- Scott Mark
- sjm@miles.com
-