Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can create a class LockCriticalSection, which lock a critical section in the constructor and unlock the critical section in the destructor.</p> <p>Then, in your code you allocate a LockCriticalSection object where you want to start the lock. The critical section will be released automatically when the object LockCriticalSection goes out of scope (because the function terminates correctly or because of an exception)</p> <p>The following is the code that takes care of locking and unlocking the critical section:</p> <pre><code>/// \brief This class locks a critical section in the /// constructor and unlocks it in the destructor. /// /// This helps to correctly release a critical section in /// case of exceptions or premature exit from a function /// that uses the critical section. /// /////////////////////////////////////////////////////////// class LockCriticalSection { public: /// \brief Creates the object LockCriticalSection and /// lock the specified CRITICAL_SECTION. /// /// @param pCriticalSection pointer to the CRITICAL_SECTION /// to lock /// /////////////////////////////////////////////////////////// LockCriticalSection(CRITICAL_SECTION* pCriticalSection): m_pCriticalSection(pCriticalSection) { EnterCriticalSection(pCriticalSection); } /// \brief Destroy the object LockCriticalSection and /// unlock the previously locked CRITICAL_SECTION. /// /////////////////////////////////////////////////////////// virtual ~LockCriticalSection() { LeaveCriticalSection(m_pCriticalSection); } private: CRITICAL_SECTION* m_pCriticalSection; }; </code></pre> <p>And this is the modified source code from your question:</p> <pre><code>do { { LockCriticalSection lock(&amp;criticalSectionToLock); while (!commandExec-&gt;commands.empty()) { commandExec-&gt;ExecuteCommand(commandExec-&gt;commands.front()); commandExec-&gt;commands.pop_front(); } } // here the critical section is released // suspend thread here } while (commandExec-&gt;maintainCommandExecution); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload