gpt4 book ai didi

c++ - 使用 _beginthreadex/_endthreadex 函数

转载 作者:行者123 更新时间:2023-11-28 01:46:12 26 4
gpt4 key购买 nike

我用分析器检查了一个项目的源代码。我是 C++ 新手。

它告诉我应该使用 Use _beginthreadex/_endthreadex 函数而不是 CreateThread/ExitThread 函数

std::unique_ptr<Thread> Thread::Create(CreationParameters params,
std::function<void()> start_routine) {
auto start_data = new ThreadStartData({std::move(start_routine)});
HANDLE handle =
CreateThread(NULL, params.stack_size, ThreadStartRoutine, start_data,
params.create_suspended ? CREATE_SUSPENDED : 0, NULL);
if (handle == INVALID_HANDLE_VALUE) {

void Thread::Exit(int exit_code) { ExitThread(exit_code); }

最佳答案

这真的取决于你在做什么。如果您只在新线程中调用 Windows API 函数,那么 CreateThread 就可以了,但如果您在线程中调用 C 标准库函数,那么您应该使用 _beginthreadex_beginthreadex 将在内部调用 CreateThread,但它也可能分配和初始化一些每线程 CRT 状态。

我在评论中看到有人声称 CRT 不再以这种方式分配 CRT 状态。我不知道这是不是真的,无论哪种方式,它都是一个实现细节,可能会根据编译器版本而改变。 _beginthreadex 永远不会出错,所以没有理由不使用它。

关于c++ - 使用 _beginthreadex/_endthreadex 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44992262/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com