FAQ 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. =========================================
  2. PTHREADS-WIN32 Frequently Asked Questions
  3. =========================================
  4. INDEX
  5. -----
  6. Q 1 What is it?
  7. Q 2 Which of the several dll versions do I use?
  8. or,
  9. What are all these pthread*.dll and pthread*.lib files?
  10. Q 3 What is the library naming convention?
  11. Q 4 Cleanup code default style or: it used to work when I built
  12. the library myself, but now it doesn't - why?
  13. Q 5 Why is the default library version now less exception-friendly?
  14. Q 6 Should I use Cygwin or Mingw32 as a development environment?
  15. Q 7 Now that pthreads-win32 builds under Mingw32, why do I get
  16. memory access violations (segfaults)?
  17. Q 8 How do I use pthread.dll for Win32 (Visual C++ 5.0)
  18. Q 9 Cancelation doesn't work for me, why?
  19. Q 10 How do I generate pthreadGCE.dll and libpthreadw32.a for use
  20. with Mingw32?
  21. Q 11 Why isn't pthread_t defined as a scalar (e.g. pointer or int)
  22. like it is for other POSIX threads implementations?
  23. =============================================================================
  24. Q 1 What is it?
  25. ---
  26. Pthreads-win32 is an Open Source Software implementation of the
  27. Threads component of the POSIX 1003.1c 1995 Standard for Microsoft's
  28. Win32 environment. Some functions from POSIX 1003.1b are also
  29. supported including semaphores. Other related functions include
  30. the set of read-write lock functions. The library also supports
  31. some of the functionality of the Open Group's Single Unix
  32. specification, version 2, namely mutex types.
  33. See the file "ANNOUNCE" for more information including standards
  34. conformance details and list of supported routines.
  35. ------------------------------------------------------------------------------
  36. Q 2 Which of the several dll versions do I use?
  37. --- or,
  38. What are all these pthread*.dll and pthread*.lib files?
  39. Simply, you only use one of them, but you need to choose carefully.
  40. The most important choice you need to make is whether to use a
  41. version that uses exceptions internally, or not (there are versions
  42. of the library that use exceptions as part of the thread
  43. cancelation and cleanup implementation, and one that uses
  44. setjmp/longjmp instead).
  45. There is some contension amongst POSIX threads experts as
  46. to how POSIX threads cancelation and exit should work
  47. with languages that include exceptions and handlers, e.g.
  48. C++ and even C (Microsoft's Structured Exceptions).
  49. The issue is: should cancelation of a thread in, say,
  50. a C++ application cause object destructors and C++ exception
  51. handlers to be invoked as the stack unwinds during thread
  52. exit, or not?
  53. There seems to be more opinion in favour of using the
  54. standard C version of the library (no EH) with C++ applications
  55. since this appears to be the assumption commercial pthreads
  56. implementations make. Therefore, if you use an EH version
  57. of pthreads-win32 then you may be under the illusion that
  58. your application will be portable, when in fact it is likely to
  59. behave very differently linked with other pthreads libraries.
  60. Now you may be asking: why have you kept the EH versions of
  61. the library?
  62. There are a couple of reasons:
  63. - there is division amongst the experts and so the code may
  64. be needed in the future. (Yes, it's in the repository and we
  65. can get it out anytime in the future, but ...)
  66. - pthreads-win32 is one of the few implementations, and possibly
  67. the only freely available one, that has EH versions. It may be
  68. useful to people who want to play with or study application
  69. behaviour under these conditions.
  70. ------------------------------------------------------------------------------
  71. Q 3 What is the library naming convention?
  72. ---
  73. Because the library is being built using various exception
  74. handling schemes and compilers - and because the library
  75. may not work reliably if these are mixed in an application,
  76. each different version of the library has it's own name.
  77. Note 1: the incompatibility is really between EH implementations
  78. of the different compilers. It should be possible to use the
  79. standard C version from either compiler with C++ applications
  80. built with a different compiler. If you use an EH version of
  81. the library, then you must use the same compiler for the
  82. application. This is another complication and dependency that
  83. can be avoided by using only the standard C library version.
  84. Note 2: if you use a standard C pthread*.dll with a C++
  85. application, then any functions that you define that are
  86. intended to be called via pthread_cleanup_push() must be
  87. __cdecl.
  88. Note 3: the intention is to also name either the VC or GC
  89. version (it should be arbitrary) as pthread.dll, including
  90. pthread.lib and libpthread.a as appropriate.
  91. In general:
  92. pthread[VG]{SE,CE,C}.dll
  93. pthread[VG]{SE,CE,C}.lib
  94. where:
  95. [VG] indicates the compiler
  96. V - MS VC
  97. G - GNU C
  98. {SE,CE,C} indicates the exception handling scheme
  99. SE - Structured EH
  100. CE - C++ EH
  101. C - no exceptions - uses setjmp/longjmp
  102. For example:
  103. pthreadVSE.dll (MSVC/SEH)
  104. pthreadGCE.dll (GNUC/C++ EH)
  105. pthreadGC.dll (GNUC/not dependent on exceptions)
  106. The GNU library archive file names have changed to:
  107. libpthreadGCE.a
  108. libpthreadGC.a
  109. ------------------------------------------------------------------------------
  110. Q 4 Cleanup code default style or: it used to work when I built
  111. --- the library myself, but now it doesn't - why?
  112. Up to and including snapshot 2001-07-12, if not defined, the cleanup
  113. style was determined automatically from the compiler used, and one
  114. of the following was defined accordingly:
  115. __CLEANUP_SEH MSVC only
  116. __CLEANUP_CXX C++, including MSVC++, GNU G++
  117. __CLEANUP_C C, including GNU GCC, not MSVC
  118. These defines determine the style of cleanup (see pthread.h) and,
  119. most importantly, the way that cancelation and thread exit (via
  120. pthread_exit) is performed (see the routine ptw32_throw() in private.c).
  121. In short, the exceptions versions of the library throw an exception
  122. when a thread is canceled or exits (via pthread_exit()), which is
  123. caught by a handler in the thread startup routine, so that the
  124. the correct stack unwinding occurs regardless of where the thread
  125. is when it's canceled or exits via pthread_exit().
  126. After snapshot 2001-07-12, unless your build explicitly defines (e.g.
  127. via a compiler option) __CLEANUP_SEH, __CLEANUP_CXX, or __CLEANUP_C, then
  128. the build now ALWAYS defaults to __CLEANUP_C style cleanup. This style
  129. uses setjmp/longjmp in the cancelation and pthread_exit implementations,
  130. and therefore won't do stack unwinding even when linked to applications
  131. that have it (e.g. C++ apps). This is for consistency with most/all
  132. commercial Unix POSIX threads implementations.
  133. Although it was not clearly documented before, it is still necessary to
  134. build your application using the same __CLEANUP_* define as was
  135. used for the version of the library that you link with, so that the
  136. correct parts of pthread.h are included. That is, the possible
  137. defines require the following library versions:
  138. __CLEANUP_SEH pthreadVSE.dll
  139. __CLEANUP_CXX pthreadVCE.dll or pthreadGCE.dll
  140. __CLEANUP_C pthreadVC.dll or pthreadGC.dll
  141. THE POINT OF ALL THIS IS: if you have not been defining one of these
  142. explicitly, then the defaults have been set according to the compiler
  143. and language you are using, as described at the top of this
  144. section.
  145. THIS NOW CHANGES, as has been explained above. For example:
  146. If you were building your application with MSVC++ i.e. using C++
  147. exceptions (rather than SEH) and not explicitly defining one of
  148. __CLEANUP_*, then __CLEANUP_C++ was defined for you in pthread.h.
  149. You should have been linking with pthreadVCE.dll, which does
  150. stack unwinding.
  151. If you now build your application as you had before, pthread.h will now
  152. set __CLEANUP_C as the default style, and you will need to link
  153. with pthreadVC.dll. Stack unwinding will now NOT occur when a
  154. thread is canceled, nor when the thread calls pthread_exit().
  155. Your application will now most likely behave differently to previous
  156. versions, and in non-obvious ways. Most likely is that local
  157. objects may not be destroyed or cleaned up after a thread
  158. is canceled.
  159. If you want the same behaviour as before, then you must now define
  160. __CLEANUP_C++ explicitly using a compiler option and link with
  161. pthreadVCE.dll as you did before.
  162. ------------------------------------------------------------------------------
  163. Q 5 Why is the default library version now less exception-friendly?
  164. ---
  165. Because most commercial Unix POSIX threads implementations don't allow you to
  166. choose to have stack unwinding. (Compaq's TRU64 Unix is possibly an exception.)
  167. Therefore, providing it in pthread-win32 as a default could be dangerous
  168. and non-portable. We still provide the choice but you must now consciously
  169. make it.
  170. WHY NOT REMOVE THE EXCEPTIONS VERSIONS OF THE LIBRARY ALTOGETHER?
  171. There are a few reasons:
  172. - because there are well respected POSIX threads people who believe
  173. that POSIX threads implementations should be exceptions-aware and
  174. do the expected thing in that context. (There are equally respected
  175. people who believe it should not be easily accessible, if it's there
  176. at all.)
  177. - because pthreads-win32 is one of the few implementations that has
  178. the choice, perhaps the only freely available one, and so offers
  179. a laboratory to people who may want to explore the effects;
  180. - although the code will always be around somewhere for anyone who
  181. wants it, once it's removed from the current version it will not be
  182. nearly as visible to people who may have a use for it.
  183. ------------------------------------------------------------------------------
  184. Q 6 Should I use Cygwin or Mingw32 as a development environment?
  185. ---
  186. Important: see Q7 also.
  187. Use Mingw32 with the MSVCRT library to build applications that use
  188. the pthreads DLL.
  189. Cygwin's own internal support for POSIX threads is growing.
  190. Consult that project's documentation for more information.
  191. ------------------------------------------------------------------------------
  192. Q 7 Now that pthreads-win32 builds under Mingw32, why do I get
  193. --- memory access violations (segfaults)?
  194. The latest Mingw32 package has thread-safe exception handling (see Q10).
  195. Also, see Q6 above.
  196. ------------------------------------------------------------------------------
  197. Q 8 How do I use pthread.dll for Win32 (Visual C++ 5.0)
  198. ---
  199. >
  200. > I'm a "rookie" when it comes to your pthread implementation. I'm currently
  201. > desperately trying to install the prebuilt .dll file into my MSVC compiler.
  202. > Could you please provide me with explicit instructions on how to do this (or
  203. > direct me to a resource(s) where I can acquire such information)?
  204. >
  205. > Thank you,
  206. >
  207. You should have a .dll, .lib, .def, and three .h files. It is recommended
  208. that you use pthreadVC.dll, rather than pthreadVCE.dll or pthreadVSE.dll
  209. (see Q2 above).
  210. The .dll can go in any directory listed in your PATH environment
  211. variable, so putting it into C:\WINDOWS should work.
  212. The .lib file can go in any directory listed in your LIB environment
  213. variable.
  214. The .h files can go in any directory listed in your INCLUDE
  215. environment variable.
  216. Or you might prefer to put the .lib and .h files into a new directory
  217. and add its path to LIB and INCLUDE. You can probably do this easiest
  218. by editing the file:-
  219. C:\Program Files\DevStudio\vc\bin\vcvars32.bat
  220. The .def file isn't used by anything in the pre-compiled version but
  221. is included for information.
  222. Cheers.
  223. Ross
  224. ------------------------------------------------------------------------------
  225. Q 9 Cancelation doesn't work for me, why?
  226. ---
  227. > I'm investigating a problem regarding thread cancelation. The thread I want
  228. > to cancel has PTHREAD_CANCEL_ASYNCHRONOUS, however, this piece of code
  229. > blocks on the join():
  230. >
  231. > if ((retv = Pthread_cancel( recvThread )) == 0)
  232. > {
  233. > retv = Pthread_join( recvThread, 0 );
  234. > }
  235. >
  236. > Pthread_* are just macro's; they call pthread_*.
  237. >
  238. > The thread recvThread seems to block on a select() call. It doesn't get
  239. > cancelled.
  240. >
  241. > Two questions:
  242. >
  243. > 1) is this normal behaviour?
  244. >
  245. > 2) if not, how does the cancel mechanism work? I'm not very familliar to
  246. > win32 programming, so I don't really understand how the *Event() family of
  247. > calls work.
  248. The answer to your first question is, normal POSIX behaviour would
  249. be to asynchronously cancel the thread. However, even that doesn't
  250. guarantee cancelation as the standard only says it should be
  251. cancelled as soon as possible.
  252. Snapshot 99-11-02 or earlier only partially supports asynchronous cancellation.
  253. Snapshots since then simulate async cancelation by poking the address of
  254. a cancelation routine into the PC of the threads context. This requires
  255. the thread to be resumed in some way for the cancelation to actually
  256. proceed. This is not true async cancelation, but it is as close as we've
  257. been able to get to it.
  258. If the thread you're trying to cancel is blocked (for instance, it could be
  259. waiting for data from the network), it will only get cancelled when it unblocks
  260. (when the data arrives). For true pre-emptive cancelation in these cases,
  261. pthreads-win32 from snapshot 2004-05-16 can automatically recognise and use the
  262. QueueUserAPCEx package by Panagiotis E. Hadjidoukas. This package is available
  263. from the pthreads-win32 ftp site and is included in the pthreads-win32
  264. self-unpacking zip from 2004-05-16 onwards.
  265. Using deferred cancelation would normally be the way to go, however,
  266. even though the POSIX threads standard lists a number of C library
  267. functions that are defined as deferred cancelation points, there is
  268. no hookup between those which are provided by Windows and the
  269. pthreads-win32 library.
  270. Incidently, it's worth noting for code portability that the older POSIX
  271. threads standards cancelation point lists didn't include "select" because
  272. (as I read in Butenhof) it wasn't part of POSIX. However, it does appear in
  273. the SUSV3.
  274. Effectively, the only mandatory cancelation points that pthreads-win32
  275. recognises are those the library implements itself, ie.
  276. pthread_testcancel
  277. pthread_cond_wait
  278. pthread_cond_timedwait
  279. pthread_join
  280. sem_wait
  281. sem_timedwait
  282. pthread_delay_np
  283. The following routines from the non-mandatory list in SUSV3 are
  284. cancelation points in pthreads-win32:
  285. pthread_rwlock_wrlock
  286. pthread_rwlock_timedwrlock
  287. The following routines from the non-mandatory list in SUSV3 are not
  288. cancelation points in pthreads-win32:
  289. pthread_rwlock_rdlock
  290. pthread_rwlock_timedrdlock
  291. Pthreads-win32 also provides two functions that allow you to create
  292. cancelation points within your application, but only for cases where
  293. a thread is going to block on a Win32 handle. These are:
  294. pthreadCancelableWait(HANDLE waitHandle) /* Infinite wait */
  295. pthreadCancelableTimedWait(HANDLE waitHandle, DWORD timeout)
  296. ------------------------------------------------------------------------------
  297. Q 10 How do I create thread-safe applications using
  298. ---- pthreadGCE.dll, libpthreadw32.a and Mingw32?
  299. This should not be a problem with recent versions of MinGW32.
  300. For early versions, see Thomas Pfaff's email at:
  301. http://sources.redhat.com/ml/pthreads-win32/2002/msg00000.html
  302. ------------------------------------------------------------------------------
  303. Q 11 Why isn't pthread_t defined as a scalar (e.g. pointer or int)
  304. like it is for other POSIX threads implementations?
  305. ----
  306. Originally pthread_t was defined as a pointer (to the opaque pthread_t_
  307. struct) and later it was changed to a struct containing the original
  308. pointer plus a sequence counter. This is allowed under both the original
  309. POSIX Threads Standard and the current Single Unix Specification.
  310. When pthread_t is a simple pointer to a struct some very difficult to
  311. debug problems arise from the process of freeing and later allocing
  312. thread structs because new pthread_t handles can acquire the identity of
  313. previously detached threads. The change to a struct was made, along with
  314. some changes to their internal managment, in order to guarantee (for
  315. practical applications) that the pthread_t handle will be unique over the
  316. life of the running process.
  317. Where application code attempts to compare one pthread_t against another
  318. directly, a compiler error will be emitted because structs can't be
  319. compared at that level. This should signal a potentially serious problem
  320. in the code design, which would go undetected if pthread_t was a scalar.
  321. The POSIX Threading API provides a function named pthread_equal() to
  322. compare pthread_t thread handles.
  323. Other pthreads implementations, such as Sun's, use an int as the handle
  324. but do guarantee uniqueness within the process scope. Win32 scalar typed
  325. thread handles also guarantee uniqueness in system scope. It wasn't clear
  326. how well the internal management of these handles would scale as the
  327. number of threads and the fragmentation of the sequence numbering
  328. increased for applications where thousands or millions of threads are
  329. created and detached over time. The current management of threads within
  330. pthreads-win32 using structs for pthread_t, and reusing without ever
  331. freeing them, reduces the management time overheads to a constant, which
  332. could be important given that pthreads-win32 threads are built on top of
  333. Win32 threads and will therefore include that management overhead on top
  334. of their own. The cost is that the memory resources used for thread
  335. handles will remain at the peak level until the process exits.
  336. While it may be inconvenient for developers to be forced away from making
  337. assumptions about the internals of pthread_t, the advantage for the
  338. future development of pthread-win32, as well as those applications that
  339. use it and other pthread implementations, is that the library is free to
  340. change pthread_t internals and management as better methods arise.