# Include "stdafx. H "# include" mfcthread. H "# include <deque> # include <iostream> # include <boost/shared_ptr.hpp> # include <boost/weak_ptr.hpp> # include <boost/enable_shared_from_this.hpp> using namespace STD; using namespace boost; # ifdef _ debug # define new debug_new # endif // unique application Program Object cwinapp theapp; void msleep (long ltime, bool bprocessmsg) {large_integer litmp; Longlong qpart1, qpart2; double dfminus, dffreq, dftim, dfspec; queryperformancefrequency (& litmp ); dffreq = (double) litmp. quadpart; queryperformancecounter (& litmp); qpart1 = litmp. quadpart; dfspec = 0.000001 * ltime; do {If (bprocessmsg = true) {MSG; peekmessage (& MSG, null, pm_remove); translatemessage (& MSG); dispatchmessa Ge (& MSG);} queryperformancecounter (& litmp); qpart2 = litmp. quadpart; dfminus = (double) (QPart2-QPart1); dftim = dfminus/dffreq;} while (dftim <dfspec);} double secondspertick = 0; Class command; Class activeobjectengine; typedef shared_ptr <command> commandptr; typedef shared_ptr <activeobjectengine> activeobjectengineptr; typedef weak_ptr <activeobjectengine> activeobjectengineweakptr; Class command: Public E Nable_shared_from_this <command> {public: Virtual void execute () = 0 ;}; class activeobjectengine: Public cwinthread {public: // member functionsactiveobjectengine (afx_threadproc pfnthreadproc ); static uint threadfunc (lpvoid PARAM); void addcommand (commandptr c) {itscommands. push_back (c);} public: deque <commandptr> itscommands; private: // the "real" Startup functionvirtual void go () ;}; activeobjectengine: Act Iveobjectengine (afx_threadproc pfnthreadproc): cwinthread (pfnthreadproc, null) // uninitialized ented constructor {m_bautodelete = false; // set the pointer to the class to be the startup value. // m_pthreadparams is uninitialized ented, // but there is no work-around.m_pThreadParams = This;} class sleepcommmand: Public command {commandptr wakeupcommand; activeobjectengineweakptr engine; double sleeptime; bool started; Large_integer lv; double start_time; public: sleepcommmand (double milliseconds, activeobjectengineweakptr E, commandptr WC): started (false) {sleeptime = milliseconds; engine = E; wakeupcommand = WC ;} virtual void execute () {queryperformancecounter (& LV); double current_time = secondspertick * lv. quadpart; If (! Started) {started = true; start_time = current_time; activeobjectengineptr strong_ptr = engine. lock (); If (strong_ptr) strong_ptr-> addcommand (partial ();} else {double elasped_time = current_time-start_time; If (elasped_time <sleeptime) {activeobjectengineptr strong_ptr =. lock (); If (strong_ptr) strong_ptr-> addcommand (shared_from_this (); msleep (50, true);} else {activeobjectengineptr strong_pt R = engine. lock (); If (strong_ptr) strong_ptr-> addcommand (commandptr (wakeupcommand) ;}}; class wakeupcommand: Public command {bool excuted; public: wakeupcommand () {excuted = false;} virtual void execute () {large_integer lv; queryperformancecounter (& LV); double current_time = secondspertick * lv. quadpart; excuted = true; // cout <"\ n ********** \ nexcuted! \ N ************ "<current_time ;}}; class delayedtyper: Public command {public: Double itsdelay; char itschar; static bool stop; static activeobjectengineptr engine; delayedtyper (double delay, char c) {itsdelay = delay; itschar = C ;}virtual void execute () {cout <itschar; If (! Stop) {delayandrepeat () ;}} void delayandrepeat () {commandptr C (New sleepcommmand (itsdelay, engine, shared_from_this ())); engine-> addcommand (c) ;}}; bool delayedtyper: Stop = false; activeobjectengineptr delayedtyper: Engine (New activeobjectengine (activeobjectengine: threadfunc); Class stopcommand: public command {public: Virtual void execute () {delayedtyper: Stop = true ;}}; int main () {large_integer lv; queryperformancefrequency (& LV ); secondspertick = 1.0/LV. quadpart; queryperformancecounter (& LV); double current_time = secondspertick * lv. quadpart; delayedtyper: Engine-> addcommand (commandptr (New delayedtyper (0.1, '1'); delayedtyper: Engine-> addcommand (commandptr (New delayedtyper (1, '2'); delayedtyper: Engine-> addcommand (commandptr (New delayedtyper (3, '3'); commandptr sleep_command (New sleepcommmand (6, delayedtyper :: engine, commandptr (New stopcommand); delayedtyper: Engine-> addcommand (sleep_command); delayedtyper: Engine-> createthread (); // shared_ptr <activeobjectengine> pthreads (New activeobjectengine (activeobjectengine: threadfunc); // verify (pthreads-> createthread (); printf ("thread launched \ n "); waitforsingleobject (delayedtyper: Engine-> m_hthread, infinite); Return 0;} // staticuint activeobjectengine: threadfunc (lpvoid N) {activeobjectengine * pthread = (activeobjectengine *) N; pthread-> go (); Return 0;} void activeobjectengine: Go () {While (itscommands. size ()> 0) {commandptr c = itscommands. front (); C-> execute (); itscommands. pop_front ();}}