|
-
July 6th, 2004, 01:52 PM
#1
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)
-
July 6th, 2004, 02:08 PM
#2
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 );
}
-
July 6th, 2004, 02:10 PM
#3
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
-
July 6th, 2004, 02:14 PM
#4
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...
-
July 6th, 2004, 02:16 PM
#5
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.
-
July 6th, 2004, 02:21 PM
#6
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
-
July 6th, 2004, 02:54 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|