Ways to create multithreading under VC6.0 and things to note

Source: Internet
Author: User

#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,&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.