CreateThread() vs AfxBeginThread()
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25

Thread: CreateThread() vs AfxBeginThread()

  1. #1
    Join Date
    Dec 2005
    Posts
    445

    CreateThread() vs AfxBeginThread()

    Hi all,

    Can anyone please tell me what is the differece between CreateThread() and AfxBeginThread(), and why should I use one on the other??

    Many thanks

  2. #2
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: CreateThread() vs AfxBeginThread()

    Don't use either. Use _beginthreadex
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,476

    Re: CreateThread() vs AfxBeginThread()

    Quote Originally Posted by souldog
    Don't use either. Use _beginthreadex
    +1

  4. #4
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: CreateThread() vs AfxBeginThread()

    Not comparing the three.
    1. CreateThread is Windows API.
    2. _beginthread[ex] is runtime library function.
    3. AfxBeginThread is MFC specific global function, which returns CWinThread*
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  5. #5
    Join Date
    Feb 2005
    Posts
    2,160

    Re: CreateThread() vs AfxBeginThread()

    Quote Originally Posted by Arjay
    +1
    +2

  6. #6
    Join Date
    Nov 2003
    Posts
    1,825

    Re: CreateThread() vs AfxBeginThread()

    Quote Originally Posted by souldog
    Don't use either. Use _beginthreadex
    Not always. If your thread uses MFC at all, you should use AfxBeginThread().

    http://msdn.microsoft.com/en-us/libr...z9(VS.80).aspx

    gg

  7. #7
    Join Date
    Apr 2000
    Location
    Frederick, Maryland
    Posts
    507

    Re: CreateThread() vs AfxBeginThread()


  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: CreateThread() vs AfxBeginThread()

    If you use pthread_create() your code will be cross-platform.

    This is, naturally, irrelevant if you're using MFC anyway.

  9. #9
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: CreateThread() vs AfxBeginThread()

    If you use pthread_create() your code will be cross-platform.
    Cross platform is not always a good solution, specially if you want to take full power of the target operating system. Performance issues would surely be there.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,476

    Re: CreateThread() vs AfxBeginThread()

    Quote Originally Posted by Codeplug
    Not always. If your thread uses MFC at all, you should use AfxBeginThread().

    http://msdn.microsoft.com/en-us/libr...z9(VS.80).aspx

    gg
    MFC windowing classes anyway. If best practices dictate that all UI should stay in the main UI thread, then there isn't any need to create additional UI threads. If this is true, you can always use _beginthreadex.

  11. #11
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: CreateThread() vs AfxBeginThread()

    Quote Originally Posted by Arjay
    MFC windowing classes anyway. If best practices dictate that all UI should stay in the main UI thread, then there isn't any need to create additional UI threads. If this is true, you can always use _beginthreadex.
    +1
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  12. #12
    Join Date
    Dec 2005
    Posts
    445

    Re: CreateThread() vs AfxBeginThread()

    Excellent input guys, very helpfull!

    (Zeeshan very nice articles).


    I have some question to make some things clearer


    1. I am using MFC and I want to build a worker thread, should I still use _beginthread[ex] ?

    2.
    Quote Originally Posted by Arjay
    MFC windowing classes anyway. If best practices dictate that all UI should stay in the main UI thread, then there isn't any need to create additional UI threads. If this is true, you can always use _beginthreadex.
    Can someone please explain me what did Arjay mean by that?


    3. Do I need to call: _endthreadex to end the thread or can I just return 0 ?


    4. Is the following code is the right implementation?

    Code:
    typedef unsigned (__stdcall *PTHREAD_START) (void *);
    
    #define BEGINTHREADEX(	pSecurity,						\
    						uiStack,						\
    						pfnStartAddr,					\
    						pArgList,						\
    						uiFlags,						\
    						puiThreadId)					\
    						((HANDLE)_beginthreadex(		\
    						(void *) (pSecurity),			\
    						(unsigned) (uiStack),			\
    						(PTHREAD_START) (pfnStartAddr), \
    						(void *) (pArgList),			\
    						(unsigned) (uiFlags),			\
    						(unsigned *) (puiThreadId)))
    
    
    
    BOOL CNtestKitDlg::OnInitDialog()
    {
    ....
    ....
    ....
    HANDLE hOutgoingThread;
    unsigned dwOutgoingThreadID;
    
    hOutgoingThread = BEGINTHREADEX( NULL,
    0,
    ThreadOutgoingProc,
    OutgoingThread,
    CREATE_SUSPENDED,
    &dwOutgoingThreadID);
    
    
    
    //Do we have our thread?
    if (!hOutgoingThread)
    {
    	AfxMessageBox( _T("Failed to create Outgoing Thread!" ));
    	PostMessage(MY_WM_QUIT);
    	return FALSE;    // Failed to create thread
    }
    
    ...
    ...
    ResumeThread(hIncomingThread);
    Code:
    unsigned __stdcall ThreadOutgoingProc(void *lpData){	
    	OutgoingThreadInfo* pData;
    	pData = (OutgoingThreadInfo*) lpData;

    5 and last.
    Do I need to close hOutgoingThread handle??
    e.g. CloseHandle(hOutgoingThread); ??

    Many many thanks!!!
    Last edited by Salvadoravi; May 15th, 2008 at 05:25 PM.

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,476

    Re: CreateThread() vs AfxBeginThread()

    See the SimpleThread articles in my subject line. They show how to start, pause, resume, and stop a thread using _beginthreadex.

    In terms of closing the handle... in general you always want to close any open handles that you have created; otherwise you'll leak system resources.

  14. #14
    Join Date
    Nov 2003
    Posts
    1,825

    Re: CreateThread() vs AfxBeginThread()

    Quote Originally Posted by MSDN
    ... any thread that uses MFC must be created by MFC. For example, a thread created by the run-time function _beginthread, _beginthreadex cannot use any MFC APIs.
    Sure, their are parts of MFC that don't rely on MFC's internal thread-local data - but why even worry about it. If you're writing MFC code, use MFC API's.

    >> 1. I am using MFC and ...
    If your thread code doesn't touch any part of MFC (and never will), then you could use _beginthreadex. Otherwise, use AfxBeginThread().

    >> 2. Can someone please explain me what did Arjay mean by that?
    >>> MFC windowing classes anyway.
    He's assuming that MFC GUI objects are the only objects/API's that (will ever) rely on MFC's internal thread-local data.

    >>> If best practices dictate that all UI should stay in the main UI thread, then there isn't any need to create additional UI threads. If this is true, you can always use _beginthreadex.
    He's implying that there's no good reason to ever have an additional UI thread, since that functionality could be implemented in the main UI thread. And building on the first assumption, if your additional threads don't use MFC GUI objects/API's, then they don't rely on MFC's internal thread-local data, and therefore it's ok to use _beginthreadex (even though the documentation says not too).

    >> 3. Do I need to ...
    You can just return. No "exit" call is needed.

    gg

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: CreateThread() vs AfxBeginThread()

    Quote Originally Posted by Ajay Vijay
    Cross platform is not always a good solution, specially if you want to take full power of the target operating system. Performance issues would surely be there.
    Probably true, but barring an obvious benefit to using a platform-dependent solution, one should default to cross-platform code whenever possible. In this particular case, I think most implementations of pthreads on Windows are just wrappers for CreateThread etc anyway.

    Since MFC is being used in this case my statement was more of an FYI than anything else, though.
    Last edited by Lindley; May 16th, 2008 at 12:07 AM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center