home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / os / msdos / programm / 12465 < prev    next >
Encoding:
Text File  |  1993-01-26  |  2.4 KB  |  62 lines

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