当我为 Windows 平台编写 C/C++ 代码时,我通常会根据需要使用 Windows API。但是当涉及到多线程时,我从 <<Windows via C/C++>
中阅读了以下引述The CreateThread function is the Windows function that creates a thread. However, if you are writing C/C++ code, you should never call CreateThread. Instead, you should use the Microsoft C++ run-time library function _beginthreadex. If you do not use Microsoft's C++ compiler, your compiler vendor will have its own alternative to CrateThread. Whatever this alternative is, you must use it.
据我所知,特定平台的语言运行时库是使用该平台的 API 实现的。我认为完全有可能从我的 C/C++ 代码中调用 CreateThread()。我确实做到了。但是我就是不明白为什么要遵循上面的规则。
非常感谢您的见解。
最佳答案
当然可能直接使用Windows API的CreateThread
。
但这会使运行时库无法获知新线程。
对于运行时库中的多线程支持(包括依赖于静态存储的函数,例如我想它包括 strtok
),您需要让运行时库了解情况,而不仅仅是知情,但部分负责,例如未能为线程分配所需的任何资源,导致线程创建失败。
干杯,
关于c++ - 关于Windows API和C/C++运行时库函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4192021/