Re: When to use CCriticalSection?
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Wed, 24 Oct 2007 19:42:39 -0400
Never.
The MFC synchronization primitives are not very well done and should be avoided.
Events are almost NEVER appropriate for synchronization between threads when you are
trying to protect shared state.
What you are doing here is a producer/consumer model with a pool of size 1 and a queue of
size 1, and special-casing it. A better practice is to actually implement queues. You
can do this in the most general case with semaphores (see my essay on semaphores on my MVP
Tips site) or by using a UI thread for the threads (they cross-post messages to indicate
state, thus they are implementing a Distributed Finite-State Machine (DFSM), or use a
model such as I/O Completion Ports used as simple queues (same DFSM model with a different
implementation).
You would use a CRITICAL_SECTION (the raw API) if it were possible to both read and write
the data concurrently. You don't need synchronization of this nature if higher-level
synchronizations prevent concurrent access.
Therefore, the special-case method with Events, where there is no need to synchronize
concurrent reading and writing because there is none, is sufficient, if rather limited.
joe
On Wed, 24 Oct 2007 10:08:59 -0700, Donos <donguy76@xxxxxxxxx> wrote:
I have an app in which there are 4 threads running simultaneously.Joseph M. Newcomer [MVP]
Right now am using "Events" to do the synchronization between
threads.
Like, there is a Server writing thread and then there is a Server
reading thread. Once Read thread, reads data from Server, it will Set
an Event for Write thread to continue.
So my question is, In this scenario do i need to use CCriticalSection?
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- When to use CCriticalSection?
- From: Donos
- When to use CCriticalSection?
- Prev by Date: Re: Microsoft MVP in ASP.NET, VC++ and/or C# needed
- Next by Date: Re: How implement combo box sliding list?
- Previous by thread: Re: When to use CCriticalSection?
- Next by thread: Re: When to use CCriticalSection?
- Index(es):
Relevant Pages
|