The Microsoft Foundation Class (MFC) library provides support for multithreaded applications. This topic describes processes and threads and the MFC 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 want. You might want to do this to handle background or maintenance tasks when you do not want the user to wait for them to complete. All threads in MFC applications are represented by objects. In most situations, you do not even have to explicitly create these objects; instead call the framework helper function , 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, because 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 might require access to the same object. Multithreading: Programming Tips describes techniques that you can use to get around problems that might arise in these situations. 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, because you must ensure that objects are not accessed by more than one thread at a time. The multithreading topics 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; however, they are only intended to be a starting point.
For more information about how the operating system handles processes and threads, see in the Platform SDK.
For more information about MFC multithreading support, see the following topics: