Skip to main content
GameDev.net gamedev.net
🔒 Locked

Multithreading

Started by TheBob Oct 12, 2005 at 12:31 PM 2 replies 1.2k views
Original Post
TheBob
TheBob
Multithread.cpp(19) : error C2664: '_beginthreadex' : cannot convert parameter 6 from 'DWORD *' to 'unsigned int *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast I have no idea what is wrong or how to fix this. Someone please help. Many Thanks Regards, TheBob
Washu
Washu
Windows expects your ThreadProc to be using the pascal calling convention, in other words, you need to stick a WINAPI in there.
DWORD WINAPI ThreadProc(  LPVOID lpParameter);
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
Agony
Agony
CreateThread() expects a very specific type of function. On the assembly level, when a function is called, the parameters and return value need to be handled certain ways. There are different methods to do so (calling conventions), but what is important is that the piece of code calling the function, and the function itself, both agree on the method. Otherwise, communication will between the two will get real nasty real quick.

CreateThread() takes a pointer to a function; this is the only information you provide as far as the function is concerned. So it has to assume that everything else works exactly the same way every time. This includes the calling convention. The assumed calling convention is __stdcall. C++ compilers by default usually use __cdecl, however. It has it's advantages (such as allowing variable-length parameter lists), but mostly it's just arbitrary. So your function that you're trying to use for your thread is __cdecl by default. What you need to do is manually force it to be __stdcall, so that CreateThread() can use it properly. To do so, all you need to do is add the __stdcall keyword right before the function name in the function declaration/definition:
//Declaration, probably in a header file:DWORD __stdcall AThreadFunction(LPVOID lpParameter);//Definition, probably in a cpp file:DWORD __stdcall AThreadFunction(LPVOID lpParameter){  while (stuff)  {    stuff;  }  return 0;}


[edit]Oh, and so as to not confuse you, there are various ways to specify __stdcall. The Win32 API has various #defines that all result in __stdcall anyway, such as WINAPI, CALLBACK, PASCAL, etcetera, sometimes depending on various settings. Any of them will typically work. I'd personally use what is specified in the documentation for a specific function. For a thread procedure, WINAPI is the #define that is used, so it'd be best to stick with this.[/edit]
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
TheBob
TheBob
Thanks. I've actually sorted the problem. I was being incredibly dumb. But thanks for the reply anyway.

TheBob

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.