Subj : Re: Question about: createthread/beginthread/beginthreadex ? To : comp.programming.threads From : m_pll Date : Tue Mar 15 2005 12:02 am mandatory wrote: > Well - now we have two different answers out of two posts? > can see why im confused ? For the sake of completeness, let me give you a third answer: use CreateThread. If you're not using any MFC code you shouldn't care about AfxBeginThread. It's true that MFC objects can only be used from threads created by MFC but that's an MFC limitation. It basically means that MFC is unsuitable for building reusable components that can be invoked on arbitrary threads. It would be ridiculous to require all win32 code in the world to link to MFC just to make this scenario work, especially when you consider the fact that many system components create threads that might call user code (e.g. things like QueueUserWorkItem, COM/RPC thread pools etc) and all of them use plain CreateThread. Now, the _beginthreadex part. If you use CRT functions from a thread created with CreateThread, there are only two cases where this could leak memory: #1. If the CRT instance that you're using is statically linked into an executable (that is, a program, not a DLL). - or - #2. If the CRT is statically linked into a DLL that has also called DisableThreadLibraryCalls in its DllMain. The recommended way to use CRT from multithreaded programs is to link to the CRT DLL (using /MD compiler option). If you do this you will have no problems with CreateThread. Even if you link statically, you will be fine as long as your module is a DLL and you don't call DisableThreadLibraryCalls. (All of this is kind of documented in CreateThread docs, even though it's a bit difficult to figure out what the're talking about) Doing #1 or #2 makes your code MFC-like in that it can only be used by threads that you created yourself. Even if everybody else was dutifully using _beginthreadex (which, again, is not the case), it wouldn't help you because *your* statically linked instance of CRT won't be notified when somebody else calls _endthreadex in *their* instance. So, to summarize, here are the rules for writing multithreaded win32 code: 1. Use /MD 2. Use CreateThread 3. Don't use MFC .