DesignConsiderations.html 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html><head><title>Design Considerations</title>
  3. <style type="text/css">
  4. <!--
  5. .style1 {font-family: "Courier New", Courier, mono}
  6. -->
  7. </style>
  8. </head><body>
  9. <h1>Design Considerations</h1>
  10. <h3>Design</h3>
  11. <p>Many of the design criteria for EA::Thread is based on the design of
  12. the Posix threading standard. The Posix threading standard is designed
  13. to work portably on a wide range of operating systems and hardware,
  14. including embedded systems and realtime environments. As such, Posix
  15. threads generally represent a competent model to follow where possible.
  16. Windows and various other platforms have independent multi-threading
  17. systems which are taken into account here as well. If something exists
  18. in Windows but doesn't exist here (e.g. Thread suspend/resume), there
  19. is a decent chance that it is by design and for some good reason.</p>
  20. <h3>C++</h3>
  21. <p>There are a number of C++ libraries devoted to multithreading. Usually
  22. the goal of these libraries is provide a platform independent interface
  23. which simplifies the most common usage patterns and helps prevent
  24. common errors. Some of these libraries are basic wrappers around
  25. existing C APIs while others (e.g. ZThreads) provide a new and
  26. different paradigm. We take the former approach here, as it is provides
  27. more or less the same functionality but provides it in a
  28. straightforward way that is easily approached by those familiar with
  29. platform-specific APIs. This approach has been referred to as the "Wrapper Facade Pattern".</p>
  30. <h3>Condition Variables / Monitors</h3>
  31. <p>Posix condition variables are implemented via the Condition class.
  32. For all practical purposes, "monitor" is the Java and C# name for Posix' condition variables. To
  33. some, a condition variable may seem similar to a Win32 Signal. In
  34. actuality they are similar but there is one critical difference: a
  35. Signal does not atomically unlock a mutex as part of the signaling
  36. process. This results in problematic race conditions that make reliable
  37. producer/consumer systems impossible to implement correctly.</p>
  38. <h3>Events / Signals</h3>
  39. <p>EAThread
  40. doesn't have an Event or Signal because it is not useful for most
  41. practical
  42. situations. You usually instead want to use a Semaphore or Condition.
  43. An
  44. Event as defined by Windows is not the same thing as a Condition
  45. (condition variable) and
  46. cannot be safely used in its place. Events cannot be used to do what a
  47. Condition does primarily due to race conditions. There may nevertheless
  48. be some use for events, though they won't be implemented in EAThread
  49. until and unless deemed useful. Given that Posix threading has
  50. undergone numerous scrutinized revisions without adding an event
  51. system, it is probably arguable that events are not necessary. A
  52. publicly available discussion on the topic of implementing events under
  53. Posix threads which could be applied to EAThread is here: <a href="http://developers.sun.com/solaris/articles/waitfor_api.html">http://developers.sun.com/solaris/articles/waitfor_api.html</a>. Check the EAThread package's scrap directory for a possible implementation of events in EAThread in the future.</p>
  54. <h3>Timeouts</h3>
  55. <p>Timeouts are specified as absolute times and not relative times. This
  56. may not be how Win32 threading works but it is what's proper and is how
  57. Posix threading works. From the OpenGroup <a href="http://www.opengroup.org/onlinepubs/007904975/functions/pthread_cond_wait.html">online</a> pthread (Posix) documentation:<br>
  58. </p>
  59. <div style="margin-left: 40px;"> An absolute time measure was chosen for
  60. specifying the timeout parameter for two reasons. First, a relative
  61. time measure can be easily implemented on top of a function that
  62. specifies absolute time, but there is a race condition associated with
  63. specifying an absolute timeout on top of a function that specifies
  64. relative timeouts. For example, assume that clock_gettime() returns the
  65. current time and cond_relative_timed_wait() uses relative timeouts:<br>
  66. <br>
  67. <span class="style1">&nbsp;&nbsp; clock_gettime(CLOCK_REALTIME, &amp;now);<br style="font-family: monospace;">
  68. &nbsp;&nbsp; reltime = sleep_til_this_absolute_time - now;<br style="font-family: monospace;">
  69. &nbsp;&nbsp; cond_relative_timed_wait(c, m, &amp;reltime);<br>
  70. </span><br style="font-family: monospace;">
  71. If the thread is preempted between the first statement and the
  72. last statement, the thread blocks for too long. Blocking, however, is
  73. irrelevant if an absolute timeout is used. An absolute timeout also
  74. need not be recomputed if it is used multiple times in a loop, such as
  75. that enclosing a condition wait. For cases when the system clock is
  76. advanced discontinuously by an operator, it is expected that
  77. implementations process any timed wait expiring at an intervening time
  78. as if that time had actually occurred.<br>
  79. </div>
  80. <br>
  81. <br>
  82. <br>
  83. </body></html>