Different threading in Visual C++

This is a discussion on Different threading in Visual C++ within the C++ Programming forums, part of the General Programming Boards category; I do not understand what is the difference between using Code: CWinThread* AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam, int nPriority = ...

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    Different threading in Visual C++

    I do not understand what is the difference between using
    Code:
     
    CWinThread* AfxBeginThread(
       AFX_THREADPROC pfnThreadProc,
       LPVOID pParam,
       int nPriority = THREAD_PRIORITY_NORMAL,
       UINT nStackSize = 0,
       DWORD dwCreateFlags = 0,
       LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL 
    );
    CWinThread* AfxBeginThread(
       CRuntimeClass* pThreadClass,
       int nPriority = THREAD_PRIORITY_NORMAL,
       UINT nStackSize = 0,
       DWORD dwCreateFlags = 0,
       LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL 
    );
    or instead using

    Code:
    HANDLE WINAPI CreateThread(
      __in_opt   LPSECURITY_ATTRIBUTES lpThreadAttributes,
      __in       SIZE_T dwStackSize,
      __in       LPTHREAD_START_ROUTINE lpStartAddress,
      __in_opt   LPVOID lpParameter,
      __in       DWORD dwCreationFlags,
      __out_opt  LPDWORD lpThreadId
    );
    in Windows XP or Windows CE

    What is the difference in practice (except for the first using an object and the second only an handle)? Are they compliant one another may be the first an enhacement of the second??

    Are they both used together with the same following function to test thread exit?
    Code:
    BOOL WINAPI GetExitCodeThread(
      __in   HANDLE hThread,
      __out  LPDWORD lpExitCode
    );
    Thank you

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    >> what is the difference
    AfxBeginThread is part of the MFC framework/library. If you use threading in an MFC application/dll, and that thread uses any other MFC objects/functions, then you should use AfxBeginThread.

    AfxBeginThread uses _beginthreadex. This is the CRT's thread creation API. If your thread uses any of CRT fucntions, then it should use _beginthreadex to avoid certain types of memory leaks.

    CreateThread is the OS interface for thread creation in user mode applications. So you have AfxBeginThread -> _beginthreadex -> CreateThread.

    For each of the interfaces, you can get the thread handle for use in Win32 API functions. With the MFC interfaces, using the raw handle safely is a little more tricky as there are some rules that need to be followed.

    gg

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by Codeplug View Post
    >> what is the difference
    AfxBeginThread is part of the MFC framework/library. If you use threading in an MFC application/dll, and that thread uses any other MFC objects/functions, then you should use AfxBeginThread.

    AfxBeginThread uses _beginthreadex. This is the CRT's thread creation API. If your thread uses any of CRT fucntions, then it should use _beginthreadex to avoid certain types of memory leaks.

    CreateThread is the OS interface for thread creation in user mode applications. So you have AfxBeginThread -> _beginthreadex -> CreateThread.

    For each of the interfaces, you can get the thread handle for use in Win32 API functions. With the MFC interfaces, using the raw handle safely is a little more tricky as there are some rules that need to be followed.

    gg

    It is quite more complicated than pthreading
    I had a look to msdn but docs there are very "disperse" One must read here and there and it's hard to have a good understanding of the subject
    Are there some tutorials / example codes you would suggest?

  4. #4
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    If I use AfxBeginThread or Createthread I can use in both cases GetExitCodeThread?
    is it compulsory to do it (like in pthread with pthread_join() ) ?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    GetExitCodeThread() is indeed the way to get the exit code of a thread (or "not yet exited" in relevant cases).

    As to which variant of thread you should use, it depends quite a bit on what you are actually doing within the thread.

    If you are using MFC, then AfxThread... is the right interface to use.

    If you are not using MFC, then _beginthread() is probably the right thing to do, unless you really do not want to call any C library functions in the thread. CreateThread() is the OS-level function that creates a thread.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    I really have to try this multithreading lark sometime.
    MSVC++ 6.0

  7. #7
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by matsp View Post
    GetExitCodeThread() is indeed the way to get the exit code of a thread (or "not yet exited" in relevant cases).

    As to which variant of thread you should use, it depends quite a bit on what you are actually doing within the thread.

    If you are using MFC, then AfxThread... is the right interface to use.

    If you are not using MFC, then _beginthread() is probably the right thing to do, unless you really do not want to call any C library functions in the thread. CreateThread() is the OS-level function that creates a thread.

    --
    Mats
    I worked on a project done by others and modified by me, using CreateThread and also C lib funcs and I noticed no problem (on W CE 4.2 with embedded Visual C ++ 4.2)

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mynickmynick View Post
    I worked on a project done by others and modified by me, using CreateThread and also C lib funcs and I noticed no problem (on W CE 4.2 with embedded Visual C ++ 4.2)
    "Noticing no problem" when crossing the road against a red-light is also possible, right? The fact that you get run over the 15th time you do this experiment doesn't preclude that the first 14 times you can get away with it.

    It is called "undefined behaviour", and may include un-noticeable side-effects.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    >> If your thread uses any of CRT fucntions, then it should use _beginthreadex to avoid certain types of memory leaks.
    AfxBeginThead leaking or not...

    >> ... and may include un-noticeable side-effects
    In this case, it's a potential memory leak. Floating point exceptions and signal() may not work as expected as well: http://www.microsoft.com/msj/1099/win32/win321099.aspx

    gg

  10. #10
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by Codeplug View Post
    >> If your thread uses any of CRT fucntions, then it should use _beginthreadex to avoid certain types of memory leaks.
    AfxBeginThead leaking or not...

    >> ... and may include un-noticeable side-effects
    In this case, it's a potential memory leak. Floating point exceptions and signal() may not work as expected as well: http://www.microsoft.com/msj/1099/win32/win321099.aspx

    gg
    Thank you very much for help
    It doesn't seem to be available _beginthreadex under Microsoft Embedded Visual C++ 4.0 While it is available AfxBeginThread
    So I guess I should always use AfxBeginThread?

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    In case you're using MFC, it wouldn't be a bad idea.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    >> It doesn't seem to be available _beginthreadex under Microsoft Embedded Visual C++ 4.0
    It was removed in CE 2.11. Under CE, just use CreateThread if not using MFC.

    gg

  13. #13
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by Codeplug View Post
    >> It doesn't seem to be available _beginthreadex under Microsoft Embedded Visual C++ 4.0
    It was removed in CE 2.11. Under CE, just use CreateThread if not using MFC.

    gg
    thank you for support
    I don't understand:
    Why should I use CreateThread() if you said it's dangerous?
    Has AfxBeginThread drawbacks?

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    It's not dangerous if you don't use MFC.
    If you use MFC, use AfxBeginThread.
    AfxBeginThread does not have any drawbacks. However, if can only be used if you use MFC.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21