Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WIN API CreateThread(<too long to list>)

Status
Not open for further replies.

EngineersForge

Technical User
Aug 26, 2003
15
CA
Could someone please point me in the right direction?

I need to find detailed documentation, beyond Platform SDK, on the CreateThread (), especially how it passes the single parameter to the instantiated Threadproc ().

Many thanks.
 
I'm a little confused as to what you want to know. The fourth parameter of the CreateThread function is passed as the argument to the ThreadProc() function.

If you are coming unstuck the following are a couple of gotchas :
You pass the address of a local variable but the creating thread exits before the new thread and so the pointer becomes invalid.
You pass data using global variables but you have multiple threads and have not synchronised thread access.
You wrap the API but dont use the this pointer to index the argument address.


If you can be a little more specific about what information you need I'm sure someone will be able to provide a more detailed response.
 
Yes those are very good points I will most definitely consider in the future. However, I was looking for an explanation as to why the fourth parameter (which is the parameter to the new thread) has to be type cast to void*. I realize it is a type LPVOID, so do you not lose all the data the original reference addresses if it is for example a complex structure???

I am not able to actually pass a parameter to the new created thread. All I have been able to do is work with global data. Not the best way to go.

Again, thanks.
 
The VOID* cast is just to make the parameter passed generic. Fundamentally a VOID* is still an address (4 Bytes) the same as a CHAR* or a mystruct*. You can think of a VOID* as bypassing the type checking so that you can pass anything you like into the function.
 
Thanks for the info. Well me get this right; one creates a type and passes it to the thread creating function where it must be type-cast to a void pointer. The new thread receives the pointer that is now pointing at an address that has data in it, but does not know the bounds of the data. So one then type-casts the void pointer back to the type it was originally and if the system is doing its thing, all the data is now properly regained???

MK
 
Pretty much, yes - the onus is on you to know the type of the argument passed and handle it correctly within the ThreadProc function.
You hand ThreadProc your pointer and then re-cast to the type you want.

The reason for prototyping the API call with a VOID * parameter is to give users flexibility - you can literally pass anything you want without the compiler throwing a wobbly. The downside of course, is that if you foul up your casts you could be in murky waters...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top