CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2003
    Location
    Pasadena, CA
    Posts
    48

    question about _beginthreadex and handles

    I'm using the _beginthreadex interface to create some threads. According to the MSDN docs _beginthreadex returns a uintptr_t, but the docs also talk about being able to directly use the handle returned by _beginthreadex in interface calls that expect HANDLE and that the user needs to perform a CloseHandle on the returned handle....

    what handle are they talking about? is the uintptr_t returned by _beginthreadex somehow convertible or dereferenceable to a handle? where is the open handle that this function is somehow supposed to return?

    can anyone explain?
    The views expressed are those of the author and do not reflect any position taken by the Goverment of the United States of America, National Aeronautics and Space Administration (NASA), Jet Propulsion Laboratory (JPL), or California Institute of Technology (CalTech)

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Cast the return to a HANDLE and use it.

    Sample from MSDN:
    Code:
    int main()
    { 
        HANDLE hThread;
        unsigned threadID;
    
        printf( "Creating second thread...\n" );
    
        // Create the second thread.
        hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );
    
        // Wait until second thread terminates. If you comment out the line
        // below, Counter will not be correct because the thread has not
        // terminated, and Counter most likely has not been incremented to
        // 1000000 yet.
        WaitForSingleObject( hThread, INFINITE );
        printf( "Counter should be 1000000; it is-> %d\n", Counter );
        // Destroy the thread object.
        CloseHandle( hThread );
    }

  3. #3
    Join Date
    Apr 2004
    Posts
    76
    Hi mclark,

    The following may prove useful: Richter Article on MSJ.

    Basically, Richter states:

    "The _beginthreadex function also returns the handle of the newly created thread just like CreateThread. So, if you have been calling CreateThread in your source code, it is fairly easy to globally replace it with calls to _beginthreadex."

    Also, I think _beginthreadex returns 0 on failure. This is different than INVALID_HANDLE_VALUE.

    Jeff

  4. #4
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Originally posted by jwalto1
    Also, I think _beginthreadex returns 0 on failure. This is different than INVALID_HANDLE_VALUE.
    But, I don't think this would be of concern since even CreateThread does not return INVALID_HANDLE_VALUE on failure. It returns NULL...

  5. #5
    Join Date
    Apr 2004
    Posts
    76
    Hi kirants,

    Thanks.

    I should have verified with the docs before making the statement.

    Jeff
    Last edited by jwalto1; July 6th, 2004 at 02:26 PM.

  6. #6
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    You raised a good point though which could have caused a problem if they didn't behave alike.

    And yes, not always one has the time to read thru documentation while answering questions in the forums. You just type in as you think , most often. At least I do that

  7. #7
    Join Date
    Nov 2003
    Location
    Pasadena, CA
    Posts
    48
    Thanks all...

    I had assumed that the typecast required meant that I had something wrong. But as it turns out using:
    reinterpret_cast<HANDLE>(::_beginthreadex())
    works just fine!

    question answered.
    The views expressed are those of the author and do not reflect any position taken by the Goverment of the United States of America, National Aeronautics and Space Administration (NASA), Jet Propulsion Laboratory (JPL), or California Institute of Technology (CalTech)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured