Is there any drawbacks for using Afxbeginthread. When should we use AfxBeginThread and when should we use CreateThread API.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Difference between Thread#run and Thread#wakeup?
- Selecting only the first few characters in a strin
- Java/Spring MVC: provide request context to child
- What exactly do pointers store? (C++)
I would never use CreateThread/CreateThread if you use even parts of the CRT, or MFC library.
It doesn't matter if you use AfxBeginThread or _beginthread or _beginthreadex. It is just a matter of taste. I prefer AfxBeginThread because I often like the CWinThread structure with InitInstance, ExitInstance and so on. And because it has less arguments ;)
The major reason is that the CRT allocates a static per thread storage that may be not freed if you simply return a thread function that was created with CreatedThread. Even using ExitThread may cause leaks.
Here is an old KB article for the reasons: http://support.microsoft.com/kb/104641/en-us
Also you can read about this in Jeffrey Richter “Advanced Windows” 3rd Edition Chapter 4, “Processes, Threads and the C Run-Time Library” Page 108ff
Or here in the CreateThread Docu: http://msdn2.microsoft.com/En-US/library/ms682453.aspx
And here in the ExitThread Docu: http://msdn2.microsoft.com/en-us/library/ms682659.aspx
For MFC programs, use
AfxBeginThread
.CreateThread
is raw Win32. It's incompatible with parts of the standard library._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 usedCreateThread
instead.AfxBeginThread
is (obviously enough) part of MFC. Along with the thread safety supported by_beginthread
, it adds some (if only a few) C++ niceties.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 thanCreateThread
.