Read this article and learn to use cevent to terminate the thread from the main thread.

Source: Internet
Author: User

Msdn library> development tools and languages ages> Visual Studio 2005> Visual Studio> visual c ++> reference> libraries reference> MFC> classes> cevent class

Collapse all

Language Filter: All

Visual Basic

C #

C ++

J #

JScript

XAML

MFC Library Reference

Cevent class

Represents an "Event"-a synchronization object that allows one thread to define y another that an event has occurred.

 
Class cevent: Public csyncobject

Remarks

Events are useful when a thread needs to know when to perform its task. for example, a thread that copies data to a data archive wowould need to be notified when new data is available. by usingCeventObject To policy the copy thread when new data is available, the thread can perform its task as soon as possible.

CeventObjects have two types: manual and automatic. A manualCeventObject stays in the state set by setevent or resetevent until the other function is called. An automaticCeventObject automatically returns to a nonsignaled (unavailable) state after at least one thread is released.

To useCeventObject, constructCeventObject when it is needed. specify the name of the event you wish to wait on, and that your application shoshould initially own it. you can then access the event when the constructor returns. call setevent to signal (make available) the event object and then call unlock when you are done accessing the controlled resource.

An alternative method for usingCeventObjects is to add a variable of TypeCeventAs a data member to the class you wish to control. during construction of the controlled object, call the constructor ofCeventData member specifying if the event is initially signaled, the type of event object you want, the name of the event (if it will be used before SS process boundaries), and desired security attributes.

to access a resource controlled by a cevent object in this manner, first create a variable of either type csinglelock or type cmultilock in your resource's access member function. then call the Lock Object's lock member function (for example, cmultilock: Lock ). at this point, your thread will either gain access to the resource, wait for the resource to be released and gain access, or wait for the resource to be released and time out, failing to gain access to the resource. in any case, your resource has been accessed in a thread-safe manner. to release the resource, call setevent to signal the event object, and then use the Lock Object's unlock member function (for example, cmultilock: Unlock), or allow the Lock Object to fall out of scope.

For more information on usingCeventObjects, see the article multithreading: How to Use the synchronization classes.

Example

 

I saw the puzzling class defination in the reference --

<A href = "http://msdn2.microsoft.com/en-us/library/efk30beh (vs.80 ). http://msdn2.microsoft.com/en-us/library/efk30beh (vs.80 ). aspx </a> [<a href = "http://msdn2.microsoft.com/en-us/library/efk30beh (vs.80 ). aspx "target =" _ blank "Title =" new window ">^</A>]

 

My question comes from the below line

 

***********************

Cprimetest (): m_pcalcnext (New cevent (false, false), m_pcalcfinished (New cevent (false, false), m_pterminatethread (New cevent (false, false )), m_icurrentprime (0)

*******************************

 

-- How to explain it.

 

-- Can we treat'm _ pcalcnext ','m _ pcalcfinished' M _ pterminatethread' and'm _ icurrentprime'As four member variables of Public type of the class

<Code>

 

Cprimetest {

Public:

{

// Create a thread that will calculate the prime numbers

Cwinthread * pthread;

Pthread =: afxbeginthread (primecalcproc, this, 0, 0, create_suincluded, null );

Pthread-> m_bautodelete = false;

Pthread-> resumethread ();

// Calcuate the first 10 prime numbers in the series on the thread

For (uint I = 0; I <10; I ++)

{

// Signal the thread to do the next work item

M_pcalcnext-> setevent ();

// Wait for the thread to complete the current task

: Waitforsingleobject (m_pcalcfinished-> m_hobject, infinite );

// Print the result

Trace ("the value of m_icurrentprime is: % d \ n", m_icurrentprime );

}

// Configure y the worker thread to exit and wait for it to complete m_pterminatethread-> setevent ();

: Waitforsingleobject (pthread-> m_hthread, infinite );

Delete pthread ;}

~ Cprimetest ()

{

Delete m_pcalcnext;

Delete m_pcalcfinished;

Delete m_pterminatethread;

}

PRIVATE:

// Determines whether the given number is a prime number static

Bool isprime (INT thisprime)

{

If (thisprime <2) return false;

For (INT n = 2; n <thisprime; n ++)

{

If (thisprime % N = 0)

Return false;

}

Return true;

}

// Calculates the next prime number in the series static

Int NextPrime (INT thisprime)

{While (true)

{If (isprime (++ thisprime ))

{Return thisprime ;}}}

// Worker thread responsible for calculating the next Prime

// Number in the Series

Static uint _ cdecl primecalcproc (lpvoid lpparameter)

{Cprimetest * pthis = static_cast <cprimetest *> (lpparameter );

Verify (pthis! = NULL); Verify (pthis-> m_pcalcnext! = NULL );

Verify (pthis-> m_pcalcfinished! = NULL );

Verify (pthis-> m_pterminatethread! = NULL );

// Create a cmultilock object to wait on the varous events

// Wait_object_0 refers to the first event in the array, wait_object_0 + 1 refers to the second

Csyncobject * pwaitobjects [] = {pthis-> m_pcalcnext, pthis-> m_pterminatethread };

Cmultilock multilock (pwaitobjects, 2L );

While (multilock. Lock (infinite, false) = wait_object_0)

{

// Calculate next Prime

Pthis-> m_icurrentprime = NextPrime (pthis-> m_icurrentprime );

// Y main thread calculation is complete

Pthis-> m_pcalcfinished-> setevent ();

}

// Terminate the thread

: Afxendthread (0, false );

Return 0l;

} Cevent * m_pcalcnext;

// Notifies worker thread to calculate next Prime

Cevent * m_pcalcfinished;

// Notifies main thread current calculation is complete

Cevent * m_pterminatethread;

// Notifies worker thread to terminate int m_icurrentprime;

// Current calculated Prime Number

};

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.