Re: When to use CCriticalSection?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.
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?
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • RE: Exchange ActiveSync does not synchronize Calendar Objexts from
    ... from SBS 2003 to you mobile device, but you can not sync calendar properly. ... Calender only from PPC to Server but not from Server to PPC ... PureMessage in Exchange Server. ... issue much possibly happens because synchronization with Mobile Device ...
    (microsoft.public.windows.server.sbs)
  • Re: Time Synchronization
    ... Locate in Client Computers in Server Management console and choose the ... Microsoft CSS Online Newsgroup Support ... >Thread-Topic: Time Synchronization ... >> Regarding the time service issue occurred on the SBS 2003 server, ...
    (microsoft.public.windows.server.sbs)
  • Re: FE/BE Replication Question
    ... The clients are thin clients that ... then direct synchronization is not advised. ... if you have multiple people logging onto a single server at ... >> - the design master and replicas are stored on the server. ...
    (microsoft.public.access.replication)
  • Re: Time server...how to set it up on FC1?
    ... > protocol used by rdate, even though they use the same port. ... but not as a server to another machine. ... Do not permit those systems to modify the ... > # Permit time synchronization with our time source, ...
    (Fedora)
  • Re: Profile path problem
    ... You experience synchronization problems if you do not disable the cache ... option for offline files on roaming user profile shares in Windows Server ... > I have problem with changing users' profile paths. ... > USERS was folder in root of local hard disk. ...
    (microsoft.public.windows.server.general)