Afxbeginthread 和 CreateThread 的区别

2022-01-12 00:00:00 multithreading c++ mfc

使用 Afxbeginthread 有什么缺点吗?我们什么时候应该使用 AfxBeginThread,什么时候应该使用 CreateThread API.

Is there any drawbacks for using Afxbeginthread. When should we use AfxBeginThread and when should we use CreateThread API.

推荐答案

对于 MFC 程序,使用 AfxBeginThread.

For MFC programs, use AfxBeginThread.

CreateThread 是原始的 Win32.它与部分标准库不兼容.

CreateThread is raw Win32. It's incompatible with parts of the standard library.

_beginthread 是 C 标准库的一部分.它添加了额外的代码来处理标准库的其他部分的线程安全,如果您使用 CreateThread 代替,这些部分将是不安全的.

_beginthread is part of the C standard library. It adds extra code to handle thread safety for other parts of the standard library that would be unsafe if you used CreateThread instead.

AfxBeginThread (显然)是 MFC 的一部分.除了 _beginthread 支持的线程安全之外,它还增加了一些(如果只有少数)C++ 细节.

AfxBeginThread is (obviously enough) part of MFC. Along with the thread safety supported by _beginthread, it adds some (if only a few) C++ niceties.

所以,如果你的程序的其余部分也是纯的、原始的 Win32,不使用标准库或 MFC,你应该只使用 CreateThread.如果您使用的是 MFC,通常应该使用 AfxBeginThread 而不是 CreateThread.

So, you should only use CreateThread if the rest of your program is also pure, raw Win32, with no use of the standard library or MFC. If you're using MFC otherwise, you should normally use AfxBeginThread rather than CreateThread.

相关文章