UserGuide.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html><head>
  3. <style type="text/css">
  4. <!--
  5. .style1 {font-family: "Courier New", Courier, mono}
  6. -->
  7. </style>
  8. </head>
  9. <body>
  10. <h1>User Guide</h1>
  11. <h3>Introduction</h3>
  12. <p>This document provides a brief description of the EAThread modules and
  13. then provides some basic information on using these modules. You will
  14. want to consult documentation for individual modules for more detailed
  15. information about them. </p>
  16. <p>All code is in C++ and largely follows the EA coding guidelines as of
  17. January of 2004. All classes are in the EA::Thread C++ namespace.
  18. Thus, the fully qualified name of the Mutex class is
  19. EA::Thread::Mutex. Most of the code is plain C++ and
  20. doesn't attempt to be very academic with the language. Thus RTTI is not
  21. used, template usage is used only in one module (FixedAllocator),
  22. exception handling is not used, etc. Unit tests have been set up for
  23. most of the functionality and are available with the full package. The
  24. headers are heavily commented in Doxygen-savvy format and the source
  25. code for the primary modules has been heavily commented as well. </p>
  26. <h3>EAThread Modules<span style="font-weight: bold;"></span>
  27. </h3>
  28. <div style="margin-left: 40px;">
  29. <table style="text-align: left; width: 100%;" border="1" cellpadding="2" cellspacing="2">
  30. <tbody>
  31. <tr>
  32. <td style="vertical-align: top;" valign="top"><span style="font-weight: bold;">Module</span><br>
  33. </td>
  34. <td style="vertical-align: top;" valign="top"><span style="font-weight: bold;">Description</span></td>
  35. <td style="vertical-align: top;" valign="top"><span style="font-weight: bold;">Source</span><br>
  36. </td>
  37. <td style="vertical-align: top;" valign="top"><span style="font-weight: bold;">Dependencies</span><br>
  38. </td>
  39. </tr>
  40. <tr>
  41. <td style="vertical-align: top;" valign="top">Thread<br>
  42. </td>
  43. <td style="vertical-align: top;" valign="top"> Implements the creation and
  44. control of individual threads. <br>
  45. </td>
  46. <td style="vertical-align: top;" valign="top">eathread.h/cpp<br>
  47. eathread_thread.h/cpp<br>
  48. </td>
  49. <td style="vertical-align: top;" valign="top">EABase</td>
  50. </tr>
  51. <tr>
  52. <td style="vertical-align: top;" valign="top">Storage<br>
  53. </td>
  54. <td style="vertical-align: top;" valign="top">Implements thread-specific
  55. storage (a.k.a. thread-local storage). This is a mechanism whereby a given
  56. named global variable exists not once globally but exists once per thread.
  57. Each thread gets its own view of the variable. <br>
  58. </td>
  59. <td style="vertical-align: top;" valign="top">eathread_storage.h/cpp<br>
  60. </td>
  61. <td style="vertical-align: top;" valign="top">EABase<br>
  62. eathead.h/cpp<br>
  63. eathread_mutex.h/cpp*<br>
  64. <br>
  65. </td>
  66. </tr>
  67. <tr>
  68. <td style="vertical-align: top;" valign="top">Atomic</td>
  69. <td style="vertical-align: top;" valign="top">Implements atomic operations
  70. on integers and pointers. These are useful for doing thread-safe basic
  71. operations and tests on integers or pointers without the cost of more
  72. expensive synchronization primitives such as mutexes.<br>
  73. </td>
  74. <td style="vertical-align: top;" valign="top">eathread_atomic.h<br>
  75. </td>
  76. <td style="vertical-align: top;" valign="top">EABase</td>
  77. </tr>
  78. <tr>
  79. <td style="vertical-align: top;" valign="top">Mutex<br>
  80. </td>
  81. <td style="vertical-align: top;" valign="top">Implements traditional mutual
  82. exclusion. Mutexes here encompass critical section functionality (a.k.a.
  83. futex) and traditional cross-process exclusion.<br>
  84. </td>
  85. <td style="vertical-align: top;" valign="top">eathread_mutex.h/cpp<br>
  86. </td>
  87. <td style="vertical-align: top;" valign="top">EABase<br>
  88. eathread.h/cpp<br>
  89. </td>
  90. </tr>
  91. <tr>
  92. <td style="vertical-align: top;" valign="top">Futex</td>
  93. <td style="vertical-align: top;" valign="top">Implements a fast mutex. A fast mutex is a mutex which can be faster because it acts entirely within user space within the current process and can possibly have some of its code inlined. </td>
  94. <td style="vertical-align: top;" valign="top">eathread_futex.h/cpp</td>
  95. <td style="vertical-align: top;" valign="top">EABase<br>
  96. eathread.h/cpp</td>
  97. </tr>
  98. <tr>
  99. <td style="vertical-align: top;" valign="top">ReadWriteMutex<br>
  100. </td>
  101. <td style="vertical-align: top;" valign="top">Implements a mutex that allows
  102. multiple concurrent reading threads but only one writing thread. This
  103. is useful for situations where one thread is updating a state but multiple
  104. threads may be reading that state.<br>
  105. </td>
  106. <td style="vertical-align: top;" valign="top">eathread_rwmutex.h/cpp<br>
  107. </td>
  108. <td style="vertical-align: top;" valign="top"><font size="-2"></font>EABase<br>
  109. eathread.h/cpp<br>
  110. eathread_atomic.h<br>
  111. eathread_condition.h/cpp </td>
  112. </tr>
  113. <tr>
  114. <td style="vertical-align: top;" valign="top">Semaphore<br>
  115. </td>
  116. <td style="vertical-align: top;" valign="top">Implements a traditional sempahore.
  117. A semaphore has zero or positive count associated with it; a thread can
  118. 'grab' the semaphore if the count is greater than zero and grabbing it
  119. reduces its count by one. When the count is zero, threads must wait until
  120. it is incremented, which can be done arbitrarily. The semaphore is the
  121. primitive upon which all other high level primitives can be constructed.<br>
  122. </td>
  123. <td style="vertical-align: top;" valign="top">eathread_semaphore.h/cpp<br>
  124. </td>
  125. <td style="vertical-align: top;" valign="top">EABase<br>
  126. eathread.h/cpp<br>
  127. eathread_atomic.h<br>
  128. </td>
  129. </tr>
  130. <tr>
  131. <td style="vertical-align: top;" valign="top">Condition<br>
  132. </td>
  133. <td style="vertical-align: top;" valign="top">Implements a condition variable,
  134. which is a synchronization primitive that supports the producer/consumer
  135. pattern. It is for all practical purposes also known as a "monitor" in
  136. Java and C#. This primitive is particularly useful for implementing efficient
  137. cross thread-messaging systems or worker thread job implementations.<br>
  138. </td>
  139. <td style="vertical-align: top;" valign="top">eathread_condition.h/cpp<br>
  140. </td>
  141. <td style="vertical-align: top;" valign="top">EABase<br>
  142. eathread.h/cpp<br>
  143. eathread_atomic.h<br>
  144. eathread_mutex.h/cpp eathread_semaphore.h/cpp<br>
  145. </td>
  146. </tr>
  147. <tr>
  148. <td style="vertical-align: top;" valign="top">Barrier<br>
  149. </td>
  150. <td style="vertical-align: top;" valign="top">Implements a cyclic barrier
  151. primitive. A barrier is a primitive which coordinates the completion of
  152. work by a predetermined number of threads. A barrier has an integer max
  153. "height" and a current height associated with it. When a thread hits the
  154. barrier, it blocks until the prescribed number of threads hit the barrier,
  155. then all are freed.<br>
  156. </td>
  157. <td style="vertical-align: top;" valign="top">eathread_barrier.h/cpp<br>
  158. </td>
  159. <td style="vertical-align: top;" valign="top">
  160. <p>EABase<br>
  161. eathread.h/cpp<br>
  162. eathread_atomic.h<br>
  163. eathread_semaphore.h/cpp<br>
  164. </p>
  165. </td>
  166. </tr>
  167. <tr>
  168. <td style="vertical-align: top;" valign="top">SpinLock<br>
  169. </td>
  170. <td style="vertical-align: top;" valign="top">Implements a traditional spin
  171. lock. A spin lock is a special kind of mutex that "spins" in a loop waiting
  172. to be able to continue instead of blocking like a mutex. A spinlock is
  173. more efficient than a mutex but it generally doesn't work unless operating
  174. on a true multi-processing system. When it does work on a true multi-processing
  175. system it is inefficient.<br>
  176. </td>
  177. <td style="vertical-align: top;" valign="top">eathread_spinlock.h<br>
  178. </td>
  179. <td style="vertical-align: top;" valign="top">EABase<br>
  180. eathread.h/cpp<br>
  181. eathread_sync.h <br>
  182. eathread_atomic.h<br>
  183. </td>
  184. </tr>
  185. <tr>
  186. <td style="vertical-align: top;" valign="top">ReadWriteSpinLock<br>
  187. </td>
  188. <td style="vertical-align: top;" valign="top">Implements a spinlock that
  189. allows multiple readers but only a single writer. Otherwise it is similar
  190. to a basic spin lock with respect to purpose and applicability. <br>
  191. </td>
  192. <td style="vertical-align: top;" valign="top">eathread_rwspinlock.h<br>
  193. </td>
  194. <td style="vertical-align: top;" valign="top">EABase<br>
  195. eathread.h/cpp<br>
  196. eathread_sync.h <br>
  197. eathread_atomic.h<br>
  198. </td>
  199. </tr><tr>
  200. <td style="vertical-align: top;" valign="top">ThreadPool<br>
  201. </td>
  202. <td style="vertical-align: top;" valign="top">Implements a "pool" of worker
  203. threads available for work. These are commonly used by server systems
  204. to spawn off client-server tasks.<br>
  205. </td>
  206. <td style="vertical-align: top;" valign="top">eathread_pool.h/cpp<br>
  207. </td>
  208. <td style="vertical-align: top;" valign="top">EABase<br>
  209. eathread.h/cpp<br>
  210. eathread_thread.h/cpp<br>
  211. eathread_condition.h/cpp<br>
  212. eathread_atomic.h<br>
  213. eathread_list.h</td>
  214. </tr><tr>
  215. <td style="vertical-align: top;" valign="top">Sync<br>
  216. </td>
  217. <td style="vertical-align: top;" valign="top">Implements memory synchronization
  218. primitives known as "fences" or "barriers" (not to be confused with thread
  219. barrier primitives). These primitives are useful on multiprocessing platforms
  220. for synchronizing the various processors' view of memory, which can become
  221. "unsynchronized" in the presence of per-processor caches. <br>
  222. </td>
  223. <td style="vertical-align: top;" valign="top">eathread_sync.h<br>
  224. </td>
  225. <td style="vertical-align: top;" valign="top">EABase</td>
  226. </tr><tr>
  227. <td style="vertical-align: top;" valign="top">shared_ptr_mt<br>
  228. shared_array_mt<br>
  229. </td>
  230. <td style="vertical-align: top;" valign="top">These are multithread-safe
  231. equivalents to regular smart pointers such as shared_ptr and shared_array.
  232. See the TL (Template Library) for implementations of the regular versions
  233. of these smart pointers.<br>
  234. </td>
  235. <td style="vertical-align: top;" valign="top">shared_ptr_mt.h<br>
  236. shared_array_mt.h</td>
  237. <td style="vertical-align: top;" valign="top">
  238. <p>eathread_atomic.h<br>
  239. eathread_mutex.h <br>
  240. </p>
  241. </td>
  242. </tr>
  243. </tbody>
  244. </table>
  245. <p>* May not be required, depending on your platform/configuration.<br>
  246. </p>
  247. </div>
  248. <h3><span style="font-weight: bold;"></span> Examples</h3>
  249. <p>We present some
  250. very basic examples of how to use some of the EAThread modules. These
  251. exemplify the simplest uses of these modules and don't go into more
  252. advanced or complicated uses. There is more functionality in each of
  253. the classes than shown; see the documentation or header files for more
  254. information. For clarity, the examples assume that the code has
  255. specified the <span style="font-family: monospace;">using EA::Thread;</span> namespace statement.<br>
  256. <br>
  257. How to create a thread.</p>
  258. <blockquote>
  259. <p class="style1">#include "eathread/eathread_thread.h"<br>
  260. <br>
  261. int ThreadFunction(void* pContext){<br>
  262. &nbsp;&nbsp; return 0;<br>
  263. }<br>
  264. <br>
  265. Thread thread;<br>
  266. thread.Begin(ThreadFunction);</p>
  267. </blockquote>
  268. <p>How to use thread-local storage.</p>
  269. <blockquote>
  270. <p class="style1">#include "eathread/eathread_storage.h"<br>
  271. <br>
  272. ThreadLocalStorage tls;<br>
  273. tls.SetValue("hello");<br>
  274. const char* pString = (const char*)tls.GetValue();</p>
  275. </blockquote>
  276. <p>How to create and use an atomic integer.</p>
  277. <blockquote>
  278. <p class="style1">#include "eathread/eathread_atomic.h"<br>
  279. <br>
  280. AtomicInteger i = 5;<br>
  281. i += 7;<br>
  282. --i;<br>
  283. if(i.SetValueConditional(3, 6))<br>
  284. &nbsp;&nbsp; printf("i was 6 and now is 3.");</p>
  285. </blockquote>
  286. <p>How to create and use a mutex.</p>
  287. <blockquote>
  288. <p class="style1">#include "eathread/eathread_mutex.h"<br>
  289. <br>
  290. Mutex mutex(NULL, true);<br>
  291. mutex.Lock();<br>
  292. mutex.Unlock();</p>
  293. </blockquote>
  294. <p>How to create and use a futex.</p>
  295. <blockquote>
  296. <p class="style1">#include "eathread/eathread_futex.h"<br>
  297. <br>
  298. Futex futex;<br>
  299. futex.Lock();<br>
  300. futex.Unlock();</p>
  301. </blockquote>
  302. <p>How to create and use a semaphore.</p>
  303. <blockquote>
  304. <p class="style1">#include "eathread/eathread_semaphore.h"<br>
  305. <br>
  306. Semaphore semaphore(NULL, true);<br>
  307. semaphore.Post();<br>
  308. semaphore.Wait();</p>
  309. </blockquote>
  310. <p>How to create and use a condition variable.</p>
  311. <blockquote>
  312. <p class="style1">#include "eathread/eathread_condition.h"<br>
  313. <br>
  314. Condition condition(NULL, true);<br>
  315. Mutex&nbsp;&nbsp;&nbsp;&nbsp; mutex(NULL, true);<br>
  316. condition.Signal();<br>
  317. condition.Wait(&amp;mutex);</p>
  318. </blockquote>
  319. <p>How to create and use a spin lock.</p>
  320. <blockquote>
  321. <p class="style1">#include "eathread/eathread_spinlock.h"<br>
  322. <br>
  323. SpinLock spinLock;<br>
  324. spinLock.Lock();<br>
  325. spinLock.Unlock();</p>
  326. </blockquote>
  327. <p>How to create and use a shared_ptr_mt.</p>
  328. <blockquote>
  329. <p><span class="style1">#include "eathread/shared_ptr_mt.h"<br>
  330. <br>
  331. shared_ptr_mt pObject(new SomeClass);<br>
  332. pObject-&gt;DoSomething();</span></p>
  333. </blockquote>
  334. <hr style="width: 100%; height: 2px;">End of document<br>
  335. <br>
  336. <br>
  337. <br>
  338. <br>
  339. <br>
  340. <br>
  341. <br>
  342. <br>
  343. <br>
  344. <br>
  345. <br>
  346. </body>
  347. </html>