#include <stdio.h>
#include <process.h>
#include <stdio.h>
#include <windows.h>
DWORD _stdcall ThreadProc (LPVOID lpparameter)//Thread execution function
{
int si=100;
while (si>0)
{
printf ("sub-thread output number:%d\n", si--);
Sleep (1000);
}
return 0;
}
int main ()
{
int mi=0;
CreateThread (null,0,threadproc,null,0,null);//Create a thread to execute the THREADPROC function
while (mi<100)
{
printf ("Main thread output number:%d\n", mi++);
Sleep (1000);
}
return 0;
}
This piece of code is fine, but if you change CreateThread () to _beginthreadex (), it will go wrong!!!
#include <stdio.h>
#include <process.h>
#include <stdio.h>
#include <windows.h>
unsigned _stdcall ThreadProc1 (LPVOID lpparameter)//thread execution function 1
{
int si=100;
while (si>0)
{
printf ("Sub-thread:%d\n", si--);
Sleep (1000);
}
return 0;
}
int main ()
{
int mi=0;
HANDLE hth1;
unsigned uithread1id;
Hth1 = (HANDLE) _beginthreadex (NULL, 0,threadproc1,null,
CREATE_SUSPENDED,&UITHREAD1ID);
if (hth1 = = NULL)
{
return FALSE;
}
Else
{
ResumeThread (HTH1);
}
while (mi<100)
{
printf ("Main thread output number:%d\n\n", mi++);
Sleep (1000);
}
return 0;
}
Error _beginthreadex () is undefined after running. This time you need to change the settings.
_beginthreadex not only to add the header file "Process.h" also set the project properties;
When writing multi-threaded program with _beginthreadex under VC6, not only to include <process.h>, but also to set the project properties, select C/S
column, select Code Generation in the category, select the Multi-threaded version (with multithreaded) on the Use Run-time Library column.
Ways to create multithreading under VC6.0 and things to note