Go: Create Multithreading in MFC

Source: Internet
Author: User

The multithreaded functions of MFC must be declared as static or global (except that the global function cannot access the private static members of the class, while the static class functions can): but such a thread function only accesses static member variables, to implement access to other members of the class, you can do this:

1) Global Function. such as UINT MyThread (lpvoid P)
2) the static member function of the class. such as the static UINT Myview::mythread (lpvoid P)

  thread functions can take parameters, such as static __stdcall Workthread (lpvoid Lpparam) you can then pass the object pointer to Lpparam when you create the thread, and then use this pointer to invoke Non-static member variables and functions in the inline Function. A thread function must be a static function because the static function of the class does not belong to any object of the class, it is the ability of the class, so you can invoke the static function of the class directly at run time to start the thread!

  Note: must be static member function, otherwise will prompt error C2664: ' CreateThread ': cannot convert parameter 3 from ' unsigned long (void *) ' to ' unsigned lo ng (__cdecl *) (void *)

AfxBeginThread and CreateThread Specific differences

1. specifically, The CreateThread function is the API function that Windows provides to the user, is the standard form of the sdk, in the process of use to take into account the process of synchronization and mutual exclusion of the relationship between processes, such as a series of synchronous mutex causes the operating system deadlock factors, More cumbersome to use, beginners in the use of the time may produce unpredictable errors, it is recommended to use AfxBeginThread, is the compiler of the original CreateThread function of the package, with MFC programming (of course, As long as the project properties are modified, both the console and the Win32 project can be Called) and _beginthread is the Run-library function of C.

When using afxbeginthread, the thread function is defined as: the UINT _yourthreadfun (lpvoid Pparam) parameter must be so when using createthread, the Thread's function is defined as: DWORD WINAPI _ Yourthreadfun (lpvoid Pparameter)

2.createthread: is a Windows API function that creates a straightforward thread. It does not consider:

1) the C runtime needs to record and initialize the multithreading to ensure that the C function library is working properly (typical example is the Strtok function).

2) MFC also needs to know the creation of new threads, and also need to do some initialization work (of course, if the use of MFC is ok). _beginthreadex:ms to the C Runtime Library extension function, first for the C Runtime library to do some initialization work to ensure that the C Runtime library is working properly. then, call CreateThread to actually create the THREAD. Afxbeginthread:mfc the function created by the thread, first creates the corresponding CWinThread object, and then calls cwinthread::createthread, in cwinthread::createthread, Completes initialization of the thread object, and then calls _beginthreadex to create the Thread.

if using MFC programming, do not use createthread, if just use the runtime Library, with _begingthread, in short , do not use CreateThread easily. this is because functions in MFC and RTL may use some of the common variables they encapsulate, meaning that both AfxBeginThread and _beginthread have their own startup code that CreateThread does not have. It is possible to have problems using Mfc's classes and RTL functions in threads created with Createthread. If you are using the assembly to write the Win32 program and do not call the MFC and RTL functions in the thread function, then use CreateThread is fine, or you are using the C write thread function, but you are careful not to call the RTL function is not a problem.

CreateThread is an interface provided by the operating system, while AfxBeginThread and _beginthread are the compiler's encapsulation of it.

Go: Create Multithreading in MFC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.