Take the 2-minute tour ×

I'm new to thread programming (currently need to use win32 and c++ in a company project) and I just want to ask does entering the critical section means no context switching? does it mean that the thread will lock the resources until it leaves the critical section?

Also, I'm currently reading "Multithreading Applications in Win32" book and it seems good but is there a better-to-read and more up-to-date book for newbies to learn threading in win32?

Many thanks in advance ^_^

share|improve this question
You probably don't need to worry about a more up-to-date book for Win32. Most of this stuff hasn't changed in over a decade. –  Jonathon Reinhart Oct 24 '13 at 7:04
Thanx Jonathon Reinhart for this piece of info, you relieved me from wasting time searching for nothing. –  M.R.M Oct 24 '13 at 7:28
add comment

1 Answer

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 your process can enter the critical section until the first thread leaves it.

From MSDN (emphasis mine):


And again, EnterCriticalSection says:


To answer the question of "will this prevent context switching between threads". No. Well, not really. Say you have two threads, A and B. A calls EnterCriticalSection and enters the CS. While he is using the shared resource in the CS, the OS can still context switch to thread B. B will continue to run as he did before, until he gets to the EnterCriticalSection call, at which point he will block.

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 EnterCriticalSection will succeed.

See also

share|improve this answer
I meant context switching between threads. I have two threads and when each one reaches a certain function to execute I don't want it to be interrupted until it finishes it. So, in this case shall I use critical section for locking (there is no shared resource, just want to make sure no switching before the thread finishes its function)? –  M.R.M Oct 24 '13 at 7:33
If there is no shared resource, then why are you trying to prevent one of the threads from running? –  Jonathon Reinhart Oct 24 '13 at 7:35
because I'm outputting to the screen and sometimes while outputting a sentence on the screen by a thread it gets Interrupted by another thread to output another sentence. So, can you give me an advice how to prevent that? –  M.R.M Oct 24 '13 at 7:52
Then your "shared resource" is the console, and you're doing the right thing by using a Critical Section :-) –  Jonathon Reinhart Oct 24 '13 at 7:53
Well, thank u very much for your fast replies ^_^. It seems then that I misused the critical section since the problem remains. Thx again Jonathan. –  M.R.M Oct 24 '13 at 8:17
show 2 more comments

Your Answer


