home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / os / os2 / programm / 7096 < prev    next >
Encoding:
Text File  |  1992-12-17  |  4.7 KB  |  120 lines

  1. Newsgroups: comp.os.os2.programmer
  2. Path: sparky!uunet!news.centerline.com!noc.near.net!mars.caps.maine.edu!gandalf!jurlwin
  3. From: jurlwin@gandalf.UMCS.Maine.EDU (Jeff Urlwin)
  4. Subject: Re: OS/2 libraries thread-safe ?
  5. Message-ID: <1992Dec17.170043.15989@gandalf.UMCS.Maine.EDU>
  6. Organization: University of Maine, Department of Computer Science
  7. References: <1992Dec16.221352.26031@cs.tu-berlin.de> <1992Dec17.094108.12612@eua.ericsson.se>
  8. Date: Thu, 17 Dec 1992 17:00:43 GMT
  9. Lines: 109
  10.  
  11. In article <1992Dec17.094108.12612@eua.ericsson.se> etxabju@eua.ericsson.se writes:
  12. >In article 26031@cs.tu-berlin.de, ernst@opal.cs.tu-berlin.de (Ernst Kloecker) writes:
  13. >>Hi,
  14. >>
  15. >>what precautions do I have to take when calling C-library functions from
  16. >>different threads in one process ?
  17. >>
  18. >What C library? I can only speek for Microsoft C 6.0. I not quite sure 
  19. >about IBM Cset/2 yet, but I think the answers are applicable for that
  20. >library as well.
  21. >
  22. >There are (at least) two versions of the library - one "normal" and one 
  23. >thread safe.  In MSC you invoke the thread safe library with -MT.
  24.  
  25. When you compile with the option /Gm+ and link with the multithreaded library,
  26. you should be OK.  You should use _beginthread() to start your thread.  It
  27. sets up the correct behavior of the library routines.
  28. (NOTE: if you use /Gm+ and have ICC call the linker for you, it will grab
  29. the multithreaded libs for you...)
  30.  
  31. >>What happens to static variables like 'errno' ?
  32. >
  33. >No problem. errno is redefined with the preprocessor to a thread-safe function:
  34. >#define errno thread_safe_errno_function() or something like that.
  35.  
  36. errno is actually a function call with the CSet/2.  It will properly deal
  37. with the multiple threads.  I forget, but I think it's defined as:
  38. extern _Optlink int *_errno(void); 
  39. #define errno  (*_errno())
  40.  
  41. Don't quote me on this, but I know I've seen this somewhere...
  42. >
  43.  
  44. >>
  45. >>Can I safely call '...printf()' from different threads ?
  46. >
  47. >Not quite sure, but I think so. printf/sprintf etc uses (in MSC) a static
  48. >result array. This array is guarded with semaphores. However, there can 
  49. >be a small risk that the output from several threads are mixed with each 
  50. >other (but only one of them calls printf() of course).
  51.  
  52. Well sprintf shouldn't be a problem, because you supply the buffer.
  53. (just don't call it with the same buffer...).  printf could either:
  54. guard the use by semaphores or dynamically allocate the memory.  Either
  55. way you *should* be OK with the call.  Where you might run into problems
  56. is when looking at the info.  If your stdout (or your FILE) is buffered
  57. in any way, you could be seeing some messed up stuff...or at least the
  58. info will be interleaved...
  59.  
  60. If this is for debugging, let me tell you what I've done in the past 
  61. (on Transputers, where you have multiple threads AND multiple processors -- 
  62. non shared memory across the processors) is to have the notion of a timestamp
  63. and do a fprintf from each thread with the timestamp, thread id (and 
  64. processor, in that case) and debugging message.  Then you can have a program
  65. merge the info into one file, based upon time.  So you might see.
  66. in file 1:
  67. 1 In thread1
  68. 4 start calculating pi
  69. 10 finished first approx
  70.  
  71. in file 2:
  72. 2 finished setup of thread 1
  73. 5 got user input
  74. 11 got finished pi...
  75.  
  76. etc.
  77.  
  78. It's pretty simple at this point to concatenate the files and sort them
  79. by time...this way you can see what's really going on...somewhat realtime.
  80.  
  81. You have to be careful, though, that you're not putting in too much
  82. printing in one thread and changing the timing of the threads and how
  83. they interact.  I've seen it happen most of the time where introducing
  84. debugging printfs, etc actually "solves" the problem....you're in for
  85. a fun hunt then!
  86. >>
  87. >>Or are the only safe functions the OS-functions in 'bsedos.h' ?
  88. >
  89. >Sorry.
  90. >
  91. >>Or are even those functions not completely thread-safe ?
  92. >>
  93. >Not absolutly sure how they are implemented, but functions like strtok(),
  94. >localtime(), gmtime() etc seems a bit dangerous - I guard them with a semaphore
  95. >when I use them.
  96. >
  97. I agree.  It's better to be careful, until you can see somewhere that they
  98. ARE re-entrant.  Maybe someone at IBM can tell us which calls are re-entrant
  99. and which aren't.  I would have to assume (until I could test further) that
  100. the Dos, Win, etc calls are re-entrant, but I would check that...
  101.  
  102. >>
  103. >>Thanks for any info, Ernst.
  104. >>--
  105. >
  106. >No problem. Hope it helped. Can someone with experience from Cset/2 and emx
  107. >tell how it's done there?
  108.  
  109. I hope someone who is more "in the know" than I am could continue this...
  110.  
  111. Does anyone have any neat tricks/techniques for debugging this stuff?
  112. Maybe the fact that my background is more in parallel processing, I don't
  113. see how people are using it here...maybe I go a little overboard...
  114.  
  115. Jeff
  116. -- 
  117. --------------------------------------
  118. jurlwin@gandalf.umcs.maine.edu
  119.  
  120.