c++ - 关于Windows API和C/C++运行时库函数的问题

标签 c++ c windows runtime

当我为 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/

相关文章:

c++ - 检查线程中的 WM_LBUTTONDOWN

c - 一个 code::blocks 项目中的多个小程序

c - 如何从 C 调用 PHP CLI?

windows - 如何在 Windows Phone 7 中绑定(bind)来自 xml 文件的动态多列表数据?

java - 在 Windows 上使用 Cygwin 排序时出现 "Input file specified two times."错误

php - 如何在 Windows 本地运行部署在 Heroku 中的 PHP 项目?

c++ - 如何完美转发结构体成员?

c++ - 在 C++ 中如何将方法指定为析构函数而不是构造函数?

c - 在Linux(RedHat)中,C函数malloc_stats()与/proc/<pid>/stat常驻内存大小相比显示不同的值

c++ - 如何在 C++ 中获取 Regex 对象的表达式字符串