Export (0) Print
Expand All
6 out of 9 rated this helpful - Rate this topic

Multithreading with C++ and MFC

The Microsoft Foundation Class Library (MFC) provides support for multithreaded applications. This article describes what processes and threads are, and MFC's approach to multithreading.

A "process" is an executing instance of an application. For example, when you double-click the Notepad icon, you start a process that runs Notepad.

A "thread" is a path of execution within a process. When you start Notepad, the operating system creates a process and begins executing the primary thread of that process. When this thread terminates, so does the process. This primary thread is supplied to the operating system by the startup code in the form of a function address. Usually, it is the address of the main or WinMain function that is supplied.

You can create additional threads in your application if you wish. You may want to do this to handle background or maintenance tasks when you don't want the user to wait for them to complete. All threads in MFC applications are represented by CWinThread objects. In most situations, you don't even have to explicitly create these objects; instead call the framework helper function AfxBeginThread, which creates the CWinThread object for you.

MFC distinguishes two types of threads: user-interface threads and worker threads. User-interface threads are commonly used to handle user input and respond to events and messages generated by the user. Worker threads are commonly used to complete tasks, such as recalculation, that do not require user input. The Win32 API does not distinguish between types of threads; it just needs to know the thread's starting address so it can begin to execute the thread. MFC handles user-interface threads specially by supplying a message pump for events in the user interface. CWinApp is an example of a user-interface thread object, as it derives from CWinThread and handles events and messages generated by the user.

Special attention should be given to situations where more than one thread may require access to the same object. The article Multithreading: Programming Tips describes techniques you can use to get around problems that may arise in these situations. The article Multithreading: How to Use the Synchronization Classes describes how to use the classes that are available to synchronize access from multiple threads to a single object.

Writing and debugging multithreaded programming is inherently a complicated and tricky undertaking, as you must ensure that objects are not accessed by more than one thread at a time. The articles in the Multithreading group do not teach the basics of multithreaded programming, only how to use MFC in your multithreaded program. The multithreaded MFC samples included in Visual C++ illustrate a few multithreaded Adding Functionality and Win32 APIs not encompassed by MFC, but are only intended to be a starting point.

For more information on how the operating system handles processes and threads, see Processes and Threads, in the Platform SDK.

For more details on MFC multithreading support, see the following articles:

See Also

Multithreading

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.