The thread may be very advanced for beginners, which is understandable. For some experienced programmers, it may be too simple. I think that people who think the thread is very simple do not understand the thread. There are too many things involved in the thread, including memory and initialization, thread synchronization. I plan to write this article in the form of QA.
Q: Why should I replace createthread with _ beginthreadex?
A: Well, the comrades who have been using API createthread to create a thread should pay attention to it. You may say that I have been using this API to create a thread, and there is no problem at all. If so, I can only say that you are lucky. Inside _ beginthreadex, it calls createthread to create a thread, while windows always uses createthread to create a thread. Before calling createthread, beginthreadex performs a lot of initialization work, so it is safer than the thread created by createthread.
Q: Why do we use two functions with the same function to treat single-thread and multi-thread sequential programs?
A: There are some historical reasons. The library of the Standard C language was invented around 1970. At that time, the concept of thread had not yet appeared in any operating system. However, the thread appears after all. Let's take a look at the following example to illustrate why the previous CRT does not support multithreading:
BOOL fFailure = (system("NOTEPAD.EXE README.TXT") == -1); if (fFailure) { switch (errno) { case E2BIG: // Argument list or environment too big break; case ENOENT: // Command interpreter cannot be found break; case ENOEXEC: // Command interpreter has bad format break; case ENOMEM: // Insufficient memory to run command break;