The flag of the Private member variable of the category class in the thread. No error is reported. It is a surprise. solve it!

Source: Internet
Author: User

 

// Testflag11.cpp: defines the entry point of the console application.
//

# Include "stdafx. H"

# Include "windows. H"

# Include "process. H"

Class exampletask
{
Public:
Exampletask (void)
{
Flag = true;
}
Static void taskmain (lpvoid PARAM );
Void starttask ();
Void setvalue (bool flag_1)
{
Flag = flag_1;
};

PRIVATE:
Bool flag;

};

Void exampletask: taskmain (lpvoid PARAM)
{
Exampletask * ptaskmain = (exampletask *) Param; // reference through the ptaskmain pointer
While (ptaskmain-> flag)
{
Printf ("Hello world \ n ");
Sleep (200 );
}

Printf ("thread exit \ n ");
Return;

}

Void exampletask: starttask ()
{
_ Beginthread (taskmain, 0, this );
}

Int main (INT argc, char * argv [])
{
Exampletask realtimetask;
Realtimetask. starttask ();

Sleep (3000 );
Realtimetask. setvalue (false );

Sleep (1000 );

Return 0;
}

 

Test results:

Private Members can be accessed in the same class!

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.