Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

MFC Multithreading

Visual Studio 2008

MFC supports multithreaded applications primarily through the use of synchronization and locking classes. Multithreaded applications depend on the CWinThread Class class. Each individual thread in an MFC application is associated with a distinct CWinThread object. This topic lists the MFC synchronization classes and then describes how CWinThread manages multithreading in MFC.

MFC provides the following synchronization classes to protect shared resources.

Class

Purpose

CSyncObject Class

The base class that provides common functionality for the synchronization classes.

CCriticalSection Class

A synchronization class that serializes access within a single process to a shared resource.

CSemaphore Class

A synchronization class that allows a limited number of concurrent accesses to a shared resource.

CMutex Class

A synchronization class that serializes access from multiple processes to a shared resource.

CEvent Class

A synchronization class that notifies an application when an event has occurred.

MFC provides the following locking classes to manage the synchronization classes.

Class

Purpose

CSingleLock Class

Used in member functions to lock one synchronization object.

CMultiLock Class

Used in member functions to lock one or more synchronization objects.

In MFC, all threads are represented by the CWinThread class. CWinThread defines and manages two different kinds of threads: worker threads and user-interface threads. The main difference between the two is that user-interface threads have a message loop and worker threads do not. Worker threads are for background tasks that do not require user input. User-interface threads process messages and can create windows. Because CWinThread is the base class for CWinApp, all MFC applications are derived from a user-interface thread object.

MFC maintains thread safety by using thread-local storage to isolate objects, like CWnd and CFont objects, to the thread that creates them. If you need to access an isolated object from another thread, you can send a user-defined message to the thread that has the object.

Every thread in an MFC application is associated and managed by its own CWinThread object. This implementation favors simplicity over robustness: the multithreading classes in MFC are not intended for complex applications. If used correctly, these classes can help you share resources in an MFC application. For more information about advanced multithreaded application development, see Multithreading with C and Win32.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.