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. Unmanaged Threading

Unmanaged Threading

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
9 Posts 7 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.
  • C Offline
    C Offline
    Chris J
    wrote on last edited by
    #1

    Does anyone know how to create a thread using an unmanaged class?

    L 1 Reply Last reply
    0
    • C Chris J

      Does anyone know how to create a thread using an unmanaged class?

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      CreateThread/Ex AfxBeginThread CWinThread

      J 1 Reply Last reply
      0
      • L led mike

        CreateThread/Ex AfxBeginThread CWinThread

        J Offline
        J Offline
        Jun Du
        wrote on last edited by
        #3

        led mike wrote:

        CreateThread/Ex

        is not thread safe. Use _beginthreadex instead. - It's easier to make than to correct a mistake.

        G J S 3 Replies Last reply
        0
        • J Jun Du

          led mike wrote:

          CreateThread/Ex

          is not thread safe. Use _beginthreadex instead. - It's easier to make than to correct a mistake.

          G Offline
          G Offline
          Graham Bradshaw
          wrote on last edited by
          #4

          Jun Du wrote:

          CreateThread/Ex is not thread safe.

          Huh? :confused:

          1 Reply Last reply
          0
          • J Jun Du

            led mike wrote:

            CreateThread/Ex

            is not thread safe. Use _beginthreadex instead. - It's easier to make than to correct a mistake.

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #5

            Jun Du wrote:

            is not thread safe. Use _beginthreadex instead.

            IF you use certain functions in the CRT, then you should use _beginthreadex. However, if you use only Win32 calls and other functions in CRT, then CreateThreadEx is perfectly fine. (As a rule, if I use any of the CRT, I use the former function, but I have used the latter in production code.) Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            A 1 Reply Last reply
            0
            • J Jun Du

              led mike wrote:

              CreateThread/Ex

              is not thread safe. Use _beginthreadex instead. - It's easier to make than to correct a mistake.

              S Offline
              S Offline
              Sarath C
              wrote on last edited by
              #6

              Jun Du wrote:

              is not thread safe. Use _beginthreadex instead.

              Who said this????? SaRath.
              "Don't Do Different things... Do Things Differently..." Understanding State Pattern in C++

              J 1 Reply Last reply
              0
              • J Joe Woodbury

                Jun Du wrote:

                is not thread safe. Use _beginthreadex instead.

                IF you use certain functions in the CRT, then you should use _beginthreadex. However, if you use only Win32 calls and other functions in CRT, then CreateThreadEx is perfectly fine. (As a rule, if I use any of the CRT, I use the former function, but I have used the latter in production code.) Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                A Offline
                A Offline
                Arvind Bharti
                wrote on last edited by
                #7

                If we are using Win32 APIs only, then CreateThread would be good enough but if we are using any CRT function that requires tiddata structure, it is must to use _beginthreadex function. Otherwise memory leak will occur because CloseHandle, corresponding to CreateThread API, won't free the dynamically allocated tiddata structure. There is one more problem related to SEH frame. For better understanding, please go through following link: http://www.microsoft.com/msj/0799/Win32/Win320799.aspx

                J 1 Reply Last reply
                0
                • A Arvind Bharti

                  If we are using Win32 APIs only, then CreateThread would be good enough but if we are using any CRT function that requires tiddata structure, it is must to use _beginthreadex function. Otherwise memory leak will occur because CloseHandle, corresponding to CreateThread API, won't free the dynamically allocated tiddata structure. There is one more problem related to SEH frame. For better understanding, please go through following link: http://www.microsoft.com/msj/0799/Win32/Win320799.aspx

                  J Offline
                  J Offline
                  Joe Woodbury
                  wrote on last edited by
                  #8

                  That's what I said. (Not all CRT functions require the local data, but knowing which ones do and which ones don't isn't generally worth the effort.) Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                  1 Reply Last reply
                  0
                  • S Sarath C

                    Jun Du wrote:

                    is not thread safe. Use _beginthreadex instead.

                    Who said this????? SaRath.
                    "Don't Do Different things... Do Things Differently..." Understanding State Pattern in C++

                    J Offline
                    J Offline
                    Jun Du
                    wrote on last edited by
                    #9

                    Using CreateThread is not thread safe, not because CreateThread is not thread safe itself, but because using it in applications linked with a CRT (C/C++ Run-Time) library is not. Some notable items: 1) The applications built on VC++ 6, regardless of type, have been linked to one of the CRT libraries shiped with VC++: Single-Threaded (default) Multithreaded Multithreaded DLL Debug Single-Threaded Debug Multithreaded Debug Multithreaded DLL You could avoid calling CRT functions in your module, but you have no control over who, where and when CRT is used in the application. 2) For multithreaded C/C++ to work properly, a data structure must be created and associated with each thread that uses CRT functions. CreateThread is an OS call, but _beginthreaded is a CRT call. If you call CreateThread, OS doesn't know: - your application is written in C/C++, - you are calling functions that aren't natively thread-safe - it needs to allocate a CRT data block for the new thread When you call _beginthreaded, CRT knows and handles all these in a proper manner. Then, CRT calls CreateThread inside _beginthreaded to actually create the thread you wanted. Jeffery Richter's classic text "Programming Applications for Microsoft Windows" (4th Ed.) Chapter 6 has explained this in more detail. - It's easier to make than to correct a mistake.

                    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