Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. difference between CreateThread & AfxBeginThread

difference between CreateThread & AfxBeginThread

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nikesh Jagtap
    wrote on last edited by
    #1

    I wanted to know what is difference between CreateThread & AfxBeginThread while creating thread? Thanks in advance Nikesh

    R 1 Reply Last reply
    0
    • N Nikesh Jagtap

      I wanted to know what is difference between CreateThread & AfxBeginThread while creating thread? Thanks in advance Nikesh

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #2

      When you're using MFC for developing applications, you should use AfxBeginThread() due to internal things in the framework that need to be set up correctly. ::CreateThread() is the Platform SDK version version when building applications with C++ without MFC support. At last a little hint, pasted from MSDN: A thread that uses functions from the C run-time libraries should use the beginthread and endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called.

      "It's supposed to be hard, otherwise anybody could do it!" - selfquote
      "High speed never compensates for wrong direction!" - unknown

      D 1 Reply Last reply
      0
      • R Roger Stoltz

        When you're using MFC for developing applications, you should use AfxBeginThread() due to internal things in the framework that need to be set up correctly. ::CreateThread() is the Platform SDK version version when building applications with C++ without MFC support. At last a little hint, pasted from MSDN: A thread that uses functions from the C run-time libraries should use the beginthread and endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called.

        "It's supposed to be hard, otherwise anybody could do it!" - selfquote
        "High speed never compensates for wrong direction!" - unknown

        D Offline
        D Offline
        Defenestration
        wrote on last edited by
        #3

        Roger Stoltz wrote:

        should use the beginthread and endthread

        You should use beginthreadex() as it gives you more control, and endthreadex() as endthread() is fundamentally flawed.

        modified on Tuesday, December 9, 2008 4:45 AM

        R 1 Reply Last reply
        0
        • D Defenestration

          Roger Stoltz wrote:

          should use the beginthread and endthread

          You should use beginthreadex() as it gives you more control, and endthreadex() as endthread() is fundamentally flawed.

          modified on Tuesday, December 9, 2008 4:45 AM

          R Offline
          R Offline
          Roger Stoltz
          wrote on last edited by
          #4

          Defenestration wrote:

          You should use beginthreadex() as it gives you more control, and endthreadex() as endthread() is fundamentally flawed.

          As I wrote, this was a quote from MSDN as a hint when using Kernel32.dll from other languages than C/C++ and a different calling convention is used. Regarding the use of any of the functions for "ending" a thread, neither of them should be used. You should simply return from the thread controlling function and if that turns out to be a problem, the design should be corrected. Regarding beginthreadex(), you may use it unless you're building an MFC application. The only correct option is to use AfxBeginThread() when building with MFC support. This is not a matter of opinion.

          "It's supposed to be hard, otherwise anybody could do it!" - selfquote
          "High speed never compensates for wrong direction!" - unknown

          D 1 Reply Last reply
          0
          • R Roger Stoltz

            Defenestration wrote:

            You should use beginthreadex() as it gives you more control, and endthreadex() as endthread() is fundamentally flawed.

            As I wrote, this was a quote from MSDN as a hint when using Kernel32.dll from other languages than C/C++ and a different calling convention is used. Regarding the use of any of the functions for "ending" a thread, neither of them should be used. You should simply return from the thread controlling function and if that turns out to be a problem, the design should be corrected. Regarding beginthreadex(), you may use it unless you're building an MFC application. The only correct option is to use AfxBeginThread() when building with MFC support. This is not a matter of opinion.

            "It's supposed to be hard, otherwise anybody could do it!" - selfquote
            "High speed never compensates for wrong direction!" - unknown

            D Offline
            D Offline
            Defenestration
            wrote on last edited by
            #5

            Roger Stoltz wrote:

            Regarding the use of any of the functions for "ending" a thread, neither of them should be used. You should simply return from the thread controlling function

            Indeed, and the only bulletproof method for knowing when a thread has finished, is to Wait...() on the thread handle. :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups