I recently wrote a small C language program needs to use multithreading, I for Multithreaded C language is a small white, looking for a variety of code examples to study, but every time I copy someone else's code to test, my vc shows error C2660: '_beginthreadex' : the function does not take 0 the parameters, this is why, my project Settings also set up a Multithreaded Debug Multithreaded, I put the code to post, please which eldest brother answers can teach me C multi-threaded use I also thank you very much, thank you very, very urgent 5555555
//crt_begthrdex. CPP
//the compile with/MT:
#include
#include
#include
Unsigned Counter;
Unsigned __stdcall SecondThreadFunc (void * pArguments)
{
Printf (" In the second thread... \n" );
While (Counter & lt; 1000000)
Counter++;
_endthreadex (0);
return 0;
}
Int main ()
{
HANDLE hThread;
Unsigned threadID;
Printf (" Creating the second thread... \n" );
//Create the second thread.
HThread=(HANDLE) _beginthreadex (NULL, 0, & amp; SecondThreadFunc, NULL, 0, & amp; ThreadID);
//Wait until the second thread terminates. If you comment out the line
//below, the Counter will not be correct because the thread from the
//terminated, and Counter most likely from the then incremented to
//1000000 yet.
The WaitForSingleObject (hThread, INFINITE);
Printf (" Counter should be 1000000; It is - & gt; % d \ n ", Counter);
//Destroy the thread object.
The CloseHandle (hThread);
return 0;
}
CodePudding user response:
You also can use CreateThreadCodePudding user response: