The underlying OS allows to pass only one parameter to a thread CreateThread:
HANDLE WINAPI CreateThread(
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in SIZE_T dwStackSize,
__in LPTHREAD_START_ROUTINE lpStartAddress,
__in_opt LPVOID lpParameter,
__in DWORD dwCreationFlags,
__out_opt LPDWORD lpThreadId
);
Accordingly the CRT thread create function allows also only one parameter, despite the name being arglist
:
uintptr_t _beginthreadex(
void *security,
unsigned stack_size,
unsigned ( *start_address )( void * ),
void *arglist,
unsigned initflag,
unsigned *thrdaddr
);
Given these restrictions the usual convention is to pass a pointer to a structure/class with all the arguments. Usually, with C++, one creates a static function that will be the thread handler and passes an instance as the argument:
class Foo
{
int _someState;
int _otherState;
char _moreState[256];
unsigned DoWork();
static unsigned ThreadHandler(void*);
public:
void StartThread();
}
void Foo::StartThread()
{
_beginthreadex(..., Foo::ThreadHandler, this, ...);
}
unsigned Foo::ThreadHandler(void* arglist)
{
Foo* pFoo = (Foo*) arglist;
return pFoo->DoWork();
}
unsigned Foo::DoWork()
{
// do here all the thread work
}
This is a fairly common idiom and in effect it allows you to pass as much state (=arguments) as needed to the thread.