Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're just a user-mode process, you can't prevent the OS from context switching to another process. What it means is no other thread in <em>your</em> process can enter the critical section until the first thread leaves it.</p> <p>From <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682608%28v=vs.85%29.aspx" rel="nofollow">MSDN</a> (emphasis mine):</p> <blockquote> <p>A thread uses the <code>EnterCriticalSection</code> or <code>TryEnterCriticalSection</code> function to request ownership of a critical section. It uses the <code>LeaveCriticalSection</code> function to release ownership of a critical section. If the critical section object is currently owned by another thread, <code>EnterCriticalSection</code> <strong>waits indefinitely for ownership</strong>.</p> </blockquote> <p>And again, <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682608%28v=vs.85%29.aspx" rel="nofollow"><code>EnterCriticalSection</code></a> says:</p> <blockquote> <p>Waits for ownership of the specified critical section object. The function returns when the calling thread is granted ownership.</p> </blockquote> <p>To answer the question of "will this prevent context switching between threads". No. Well, not really. Say you have two threads, <strong>A</strong> and <strong>B</strong>. <strong>A</strong> calls <code>EnterCriticalSection</code> and enters the CS. While he is using the shared resource in the CS, the OS can still context switch to thread <strong>B</strong>. <strong>B</strong> will continue to run as he did before, until he gets to the <code>EnterCriticalSection</code> call, at which point he will block.</p> <p>Now how this blocking is implemented really up to Windows. But most likely, instead of "spinning" (Can I enter? No. Now? No. Now? No.) the OS will put that thread on "blocked" queue, and not schedule the thread until the thing he is waiting on (the CS) is available. At that point, he will be scheduled, and the call to <code>EnterCriticalSection</code> will succeed. </p> <p>See also</p> <ul> <li>MSDN: <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms686908%28v=vs.85%29.aspx" rel="nofollow">Using Critical Section Objects</a></li> <li>Wikipedia: <a href="http://en.wikipedia.org/wiki/Critical_section" rel="nofollow">Critical Section</a></li> </ul>
    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. 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