I am working on a class project and trying to use _beginthreadex
as part of a multiprocessing project.
This is the trouble spot. I have this function that I call when using _beginthreadex
:
DWORD WINAPI threadWork(LPVOID threadNo)
{
int threadID = *(int*)threadNo;
for (unsigned int i = 0; i < numberIter; i++)
and I call the function here:
threadIDs[i] = i;
threads[i] = (HANDLE)_beginthreadex(NULL, 0, threadWork, &threadIDs[i], 0, NULL);
but I keep receiving this error:
'uintptr_t _beginthreadex(void *,unsigned int,_beginthreadex_proc_type,void *,unsigned int,unsigned int *)': cannot convert argument 3 from 'DWORD (__cdecl *)(LPVOID)' to '_beginthreadex_proc_type'
I have done a lot of searching on this topic, and am still very confused and cannot find a solution. I tried replacing LPVOID threadNo
with void *
as per another thread, but that did not solve my issue. I am assuming it is a pointer issue, but I don't know what I am missing.
_beginthreadex()
is part of Microsoft's C library runtime, not the Win32 API. But you are trying to use a Win32-style callback with it (specifically, aThreadProc
callback forCreateThread()
).If you read the
_beginthreadex()
documentation, it tells you the correct signature to use for your callback:So, use this instead:
But, if your goal really is "learning to use the Win 32 API" then use
CreateThread()
instead:That being said, since your question is tagged [c++], you really should use
std::thread
orstd::jthread
instead (unless you are stuck using a pre-2011 compiler):