windows.critical_section.h.xml 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <chapter xml:id="windows.critical_section.h">
  2. <title><tt>__vic/windows/critical_section.h</tt></title>
  3. <chapter xml:id="windows--CriticalSection">
  4. <title><tt>windows::CriticalSection</tt></title>
  5. <code-block lang="C++"><![CDATA[
  6. class windows::CriticalSection : private non_copyable
  7. {
  8. public:
  9. CriticalSection();
  10. explicit CriticalSection(DWORD dwSpinCount); // _WIN32_WINNT >= 0x0403
  11. ~CriticalSection();
  12. void Enter();
  13. bool TryEnter();
  14. void Leave() noexcept;
  15. ::CRITICAL_SECTION *handle();
  16. const ::CRITICAL_SECTION *handle() const;
  17. };
  18. ]]></code-block>
  19. <p>A C++ wrapper for Win32 API <tt>CRITICAL_SECTION</tt>.</p>
  20. <section><title>Class members</title>
  21. <synopsis>
  22. <prototype>CriticalSection()</prototype>
  23. <p>Calls <tt>::InitializeCriticalSection()</tt>.</p>
  24. </synopsis>
  25. <synopsis>
  26. <prototype>explicit CriticalSection(DWORD dwSpinCount) <sign>_WIN32_WINNT >= 0x0403</sign></prototype>
  27. <p>Calls <tt>::InitializeCriticalSectionAndSpinCount()</tt>.</p>
  28. </synopsis>
  29. <synopsis>
  30. <prototype>~CriticalSection()</prototype>
  31. <p>Calls <tt>::LeaveCriticalSection()</tt>.</p>
  32. </synopsis>
  33. <synopsis>
  34. <prototype>void Enter()</prototype>
  35. <p>Calls <tt>::EnterCriticalSection()</tt>.</p>
  36. </synopsis>
  37. <synopsis>
  38. <prototype>bool TryEnter()</prototype>
  39. <p>Calls <tt>::TryEnterCriticalSection()</tt> and returs the result.</p>
  40. </synopsis>
  41. <synopsis>
  42. <prototype>void Leave() noexcept</prototype>
  43. <p>Calls <tt>::LeaveCriticalSection()</tt>.</p>
  44. </synopsis>
  45. </section>
  46. <note>Direct calls of <tt>Enter()</tt>/<tt>Leave()</tt> should be avoided, use
  47. <xref to="windows--CSGuard"/> objects instead.</note>
  48. </chapter>
  49. <chapter xml:id="windows--CSGuard">
  50. <title><tt>windows::CSGuard</tt></title>
  51. <code-block lang="C++"><![CDATA[
  52. class windows::CSGuard : private non_copyable
  53. {
  54. public:
  55. enum adopt_t { adopt };
  56. explicit CSGuard(::CRITICAL_SECTION &cs);
  57. CSGuard(::CRITICAL_SECTION &cs, adopt_t);
  58. explicit CSGuard(CriticalSection &cs);
  59. CSGuard(CriticalSection &cs, adopt_t);
  60. ~CSGuard();
  61. };
  62. ]]></code-block>
  63. <p>A scoped lock guard. Works with <tt>::CRITICAL_SECTION</tt> as well as with
  64. <xref to="windows--CriticalSection"/> objects.</p>
  65. <section><title>Class members</title>
  66. <synopsis>
  67. <prototype>explicit CSGuard(::CRITICAL_SECTION &amp;cs)</prototype>
  68. <prototype>explicit CSGuard(CriticalSection &amp;cs)</prototype>
  69. <p>Calls <tt>::EnterCriticalSection(&amp;cs)</tt>.</p>
  70. </synopsis>
  71. <synopsis>
  72. <prototype>CSGuard(::CRITICAL_SECTION &amp;cs, adopt_t)</prototype>
  73. <prototype>CSGuard(CriticalSection &amp;cs, adopt_t)</prototype>
  74. <p>Only saves the reference to <tt>cs</tt> without
  75. <tt>::EnterCriticalSection()</tt> call.</p>
  76. </synopsis>
  77. <synopsis>
  78. <prototype>~CSGuard()</prototype>
  79. <p>Calls <tt>::LeaveCriticalSection(&amp;cs)</tt>.</p>
  80. </synopsis>
  81. </section>
  82. <section><title>Example</title>
  83. <code-block lang="C++">
  84. // Typical usage
  85. void reentrant_function()
  86. {
  87. static __vic::windows::CriticalSection cs;
  88. __vic::windows::CSGuard guard(cs);
  89. // Critical section code until the end of the block
  90. ...
  91. }
  92. // Usage of the adopt-constructor
  93. __vic::windows::CriticalSection cs;
  94. if(cs.TryEnter()) // Try to enter the critical section
  95. {
  96. // Success
  97. using __vic::windows::CSGuard;
  98. CSGuard guard(cs, CSGuard::adopt);
  99. // Critical section code until the end of the block
  100. ...
  101. }
  102. else
  103. {
  104. // The critical section is busy
  105. ...
  106. }
  107. </code-block>
  108. </section>
  109. </chapter>
  110. </chapter>