Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Afxbeginthread and CreateThread

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

like image 310
CodeRider Avatar asked Feb 12 '14 04:02

CodeRider


2 Answers

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 used CreateThread 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 than CreateThread.

like image 146
Jerry Coffin Avatar answered Sep 26 '22 22:09

Jerry Coffin


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

A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multi-threaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.

And here in the ExitThread Docu: http://msdn2.microsoft.com/en-us/library/ms682659.aspx

thread in an executable that is linked to the static C run-time library (CRT) should use _beginthread and _endthread for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when the thread calls ExitThread. Another work around is to link the executable to the CRT in a DLL instead of the static CRT. Note that this memory leak only occurs from a DLL if the DLL is linked to the static CRT and a thread calls the DisableThreadLibraryCalls function. Otherwise, it is safe to call CreateThread and ExitThread from a thread in a DLL that links to the static CRT.

like image 36
xMRi Avatar answered Sep 24 '22 22:09

xMRi