In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to create a thread with _ beginthreadex ()". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to create a thread with _ beginthreadex ()".
First, use _ beginthreadex ()
The required header file supports # include / / for _ beginthread ()
Required settings: project setting-- > C _ User run-time library selected Debug Multithreaded or Multithreaded. Even if you use MT or MTD.
The code is as follows:
# include # include / / for STL string class#include / / for HANDLE#include / / for _ beginthread () using namespace std;class ThreadX {private: int loopStart; int loopEnd; int dispFrequency;public: string threadName; ThreadX (int startValue, int endValue, int frequency) {loopStart = startValue; loopEnd = endValue; dispFrequency = frequency;} static unsigned _ stdcall ThreadStaticEntryPoint (void * pThis) {ThreadX* pthX = (ThreadX*) pThis / / the tricky cast pthX- > ThreadEntryPoint (); / / now call the true entry-point-function return 1; / / the thread exit code} void ThreadEntryPoint () {for (int I = loopStart; i threadName = "T1"; ThreadX * O2 = new ThreadX (- 100000, 0, 2000); HANDLE hth3; unsigned uiThread2ID Hth3 = (HANDLE) _ beginthreadex (NULL, / / security 0, / / stack size ThreadX::ThreadStaticEntryPoint, O2, / / arg list CREATE_SUSPENDED / / so we can later call ResumeThread () & uiThread2ID) If (hth3 = = 0) printf ("Failed to create thread 2\ n"); GetExitCodeThread (hth3, & dwExitCode); / / should be STILL_ACTIVE = 0x00000103 = 259 printf ("initial thread 2 exit code =% u\ n", dwExitCode); O2-> threadName = "T2"; ResumeThread (hth2); / / serves the purpose of Jaeschke's T1-> Start () ResumeThread (hth3); WaitForSingleObject (hth2, INFINITE); WaitForSingleObject (hth3, INFINITE) GetExitCodeThread (hth2, & dwExitCode); printf ("thread 1 exited with code% u\ n", dwExitCode); GetExitCodeThread (hth3, & dwExitCode); printf ("thread 2 exited with code% u\ n", dwExitCode); CloseHandle (hth2); CloseHandle (hth3); delete o1; o1 = NULL; delete O2; O2 = NULL; printf ("Primary thread terminating.\ n"); return 0;}
Note:
(1) CreateThread should never be called if you are writing C _ Candle + code. Instead, you should use the VisualC++ runtime library function _ beginthreadex, and exit should also use _ endthreadex. If you don't use Microsoft's VisualC++ compiler, your compiler vendor has its own CreateThread replacement function. Whatever this alternative function is, you have to use it.
(2) because _ beginthreadex and _ endthreadex are CRT thread functions, you must pay attention to the compilation option runtimelibaray, using MT or MTD. [MultiThreaded, Debug MultiThreaded] .
(3) the parameter list of the _ beginthreadex function is the same as that of the CreateThread function, but the parameter name and type are not exactly the same. This is because the development team of Microsoft's CCompact + run-time library believes that the CAccord + runtime function should not have any dependency on the Windows data type. The _ beginthreadex function, like CreateThread, returns a handle to the newly created thread.
(4) the termination of the C++ main thread will also terminate all child threads created by the main thread, regardless of whether the child thread has finished execution. So if WaitForSingleObject is not called in the above code, the two child threads T1 and T2 may not have finished execution or not at all.
(5) if a thread is suspended and then a call to WaitForSingleObject waits for the thread, it will result in a deadlock. So if the above code does not call resumethread, it will deadlock.
2. The difference between _ beginthreadex () and generation CreateThread ()
CreateThread is Windows's API function (the standard form of the SDK function, a straightforward way to create it, which can be used on any occasion), provides operating system-level operations to create threads, and is limited to worker threads. When not calling the functions of MFC and RTL, you can use CreateThread, not easily in other cases. The relationship between synchronization and mutual exclusion of the process should be taken into account in the process of use (to prevent deadlocks). The thread function is defined as: DWORD WINAPI _ yourThreadFun (LPVOID pParameter). But it does not consider:
(1) multithreading needs to be recorded and initialized in C Runtime to ensure that the C function library works properly (a typical example is the strtok function).
(2) MFC also needs to know about the creation of new threads and do some initialization work (of course, it's fine if you don't use MFC).
The MFC function created by the thread in AfxBeginThread:MFC first creates the corresponding CWinThread object, then calls CWinThread::CreateThread, initializes the thread object in CWinThread::CreateThread, and then calls _ beginthreadex (which is safer than AfxBeginThread) to create the thread. It simplifies operations or enables threads to respond to messages, either for interface threads or for worker threads, but be careful not to use _ beginthreadex () or CreateThread () in an MFC program. The thread function is defined as: UINT
_ yourThreadFun (LPVOID pParam)
_ beginthreadex:MS for the extended SDK function of C Runtime library, first of all, some initialization work has been done for C Runtime library to ensure that C Runtime library works normally. Then, call CreateThread to actually create the thread. When using only Runtime Library, you can use _ BegingThread.
Section: in fact, there is a certain calling relationship between these three functions, the first one is more pure, and after the latter two complete their corresponding work, the former is called to create the thread. CreateThread is the interface provided by the operating system, while AfxBeginThread and _ BeginThread are encapsulated by the compiler.
Section: using _ beginthreadex () and _ endthreadex functions should be the best choice, and they are all functions in C Run-time Library. The parameters and data types of the functions are all types in C Run-time Library, so that there is no need to convert between Windows data types and C Run-time Library data types when starting a thread, thus reducing the resource consumption and time consumption when starting a thread. But with _ beginthread, you cannot create a new thread with a security attribute, create a paused thread, or get a thread ID,_endthread. Similarly, it takes no parameters, which means that the exit code of this thread must be hard-coded to 0.
Section: MFC is also a C++ class library (only Microsoft's C++ class library, not the standard C++ class library), and new and delete operators are encapsulated in MFC, so you don't have to use the _ beginthreadex () function when you use new and delete, you can use the other two functions.
_ beginthreadex and _ beginthread initialize some thread-related CRT before calling back the entry function.
The function library of CRT already exists before the emergence of threads, so the original CRT cannot really support threads.
This also leads to many CRT functions in the case of multithreading must have special support, can not simply use CreateThread on OK.
Add: _ beginthreadex () is a thread function for CRT. If you want to use the function of CRT in a thread, you'd better start the thread with this. If you don't use this, there will be a memory leak.
Thank you for reading, the above is the content of "how to create a thread with _ beginthreadex ()". After the study of this article, I believe you have a deeper understanding of the problem of how to create a thread with _ beginthreadex (), and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un