mutex.h.xml 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <chapter xml:id="mutex.h">
  2. <title><tt>__vic/mutex.h</tt></title>
  3. <chapter xml:id="mutex">
  4. <title><tt>mutex</tt></title>
  5. <code-block lang="C++">
  6. class mutex : private non_copyable
  7. {
  8. public:
  9. mutex();
  10. ~mutex();
  11. void lock();
  12. bool try_lock();
  13. bool unlock() noexcept;
  14. };
  15. </code-block>
  16. <p>Plain non-recursive mutex.</p>
  17. <section><title>Usage notes</title>
  18. <p>In most cases explicit usage of <tt>lock()</tt> and <tt>unlock()</tt> should
  19. be avoided. Use class <xref to="mutex_lock"/> to manage locks instead. It provides
  20. exception safety and it's handy for usage.</p>
  21. <p>In C++11 mode <tt>std::mutex</tt> can be a better alternative.</p>
  22. </section>
  23. <section><title>Class members</title>
  24. <synopsis>
  25. <prototype>mutex()</prototype>
  26. <p>Creates unlocked mutex.</p>
  27. </synopsis>
  28. <synopsis>
  29. <prototype>~mutex()</prototype>
  30. <p>Destroys the mutex.</p>
  31. </synopsis>
  32. <synopsis>
  33. <prototype>void lock()</prototype>
  34. <p>Acquires the mutex. Waits until released if acquired by other
  35. thread at the moment.</p>
  36. </synopsis>
  37. <synopsis>
  38. <prototype>bool try_lock()</prototype>
  39. <p>Tries to acquire the mutex. Immediately returns <tt>false</tt> if it's
  40. already acquired by another thread, without waiting.</p>
  41. </synopsis>
  42. <synopsis>
  43. <prototype>bool unlock() noexcept</prototype>
  44. <p>Releases the mutex acquired before. In some cases can return <tt>false</tt>
  45. in case of error, but in general error detection is not guaranteed.</p>
  46. </synopsis>
  47. </section>
  48. <section><title>Example</title>
  49. <p>See <xref to="mutex_lock"/>.</p>
  50. </section>
  51. </chapter>
  52. <chapter xml:id="mutex_lock">
  53. <title><tt>mutex_lock</tt></title>
  54. <code-block lang="C++"><![CDATA[
  55. class mutex_lock : private non_copyable
  56. {
  57. public:
  58. enum adopt_t { adopt };
  59. explicit mutex_lock(mutex &mtx);
  60. mutex_lock(mutex &mtx, adopt_t);
  61. ~mutex_lock();
  62. };
  63. ]]></code-block>
  64. <p>Manages the lock on a mutex. The lock exists while the object is alive.</p>
  65. <section><title>Class members</title>
  66. <synopsis>
  67. <prototype>adopt</prototype>
  68. <p>Constructor tag, suppresses the mutex acquisition.</p>
  69. </synopsis>
  70. <synopsis>
  71. <prototype>explicit mutex_lock(mutex &amp;mtx)</prototype>
  72. <p>Acquires <tt>mtx</tt>.</p>
  73. </synopsis>
  74. <synopsis>
  75. <prototype>~mutex_lock()</prototype>
  76. <p>Releases <tt>mtx</tt>.</p>
  77. </synopsis>
  78. <synopsis>
  79. <prototype>mutex_lock(mutex &amp;mtx, adopt_t)</prototype>
  80. <p>Adopts already acquired <tt>mtx</tt>. See the example.</p>
  81. </synopsis>
  82. </section>
  83. <section><title>Example</title>
  84. <code-block lang="C++">
  85. // Typical usage
  86. __vic::mutex mtx;
  87. void reentrant_function()
  88. {
  89. __vic::mutex_lock lock(mtx);
  90. // Critical section code until the end of the block
  91. ...
  92. }
  93. // Usage of non-acquiring constructor
  94. if(mtx.try_lock()) // Try to acquire the mutex
  95. {
  96. // The mutex has been successfully acquired
  97. __vic::mutex_lock lock(mtx, __vic::mutex_lock::adopt);
  98. // Critical section code until the end of the block
  99. ...
  100. }
  101. else
  102. {
  103. // The mutex is acquired by another thread
  104. ...
  105. }
  106. </code-block>
  107. </section>
  108. </chapter>
  109. </chapter>