In examples on the web, calls to CreateThread typically pass a pointer to a struct for LPVOID lpParameter, and you use that pointer to access the struct itself.
#include <Windows.h>
#include <stdio.h>
struct Point
{
float x,y,z ;
} ;
DWORD WINAPI threadStartPoint( LPVOID data )
{
Sleep( 1000 ) ;
Point *p = (Point*)data ;
printf( "%f %f %f\n", p->x, p->y, p->z ) ;
puts( "Thread job done" ) ;
return 0 ;
}
// From main
int main()
{
DWORD threadId ;
Point p ;
p.x=2, p.y=3, p.z=4 ;
HANDLE handle = CreateThread( 0, 0,
threadStartPoint,
(LPVOID)&p,
0, // ?? I think I should be using this parameter</b>
&threadId
) ;
if( !handle )
{
// Thread creation failed
puts( "start fail\n" );
}
else
{
printf( "started on threadid=%d\n", threadId ) ;
}
WaitForSingleObject( handle, 2000 ) ; // wait up to 2000 ms for the other thread to complete before moving on
puts( "main thread Exiting.." ) ;
//system( "pause" ) ;
}
I'm finding this to be somewhat of an inconvenience, since you have to make sure that struct exists, and make sure it is properly destroyed when the thread is finished executing.
I'd like to start my thread, but passing normal stack arguments ie automatic variables, or perhaps, the struct itself to the thread start routine:
DWORD threadStartPointFuncStyleIWant( Data d ) ;
So my questions are really:
- For a thread start point (CreateThread), are we restricted to a function with prototype of the form:
DWORD validThreadFunc( LPVOID pParamStruct ) ;
- Or can we start a thread on functions like
DWORD threadFunc1( int p1, int p2 ) ; DWORD threadFunc2( Data d ) ;