Use boost thread timer as background thread to toggle the main Loop program State method Summary

Source: Internet
Author: User

1: Simple understanding of Boost timer

#include"stdafx.h"#include<string>#include<boost\thread.hpp>#include<boost/asio.hpp>#include<iostream>using namespaceBoost::asio;using namespaceboost;intExit1 =1;voidPrintingint&Len) {this_thread::sleep_for (Chrono::seconds (len));//change System state after blocking timer in threadlen++; Exit1=0;}int_tmain (intARGC, _tchar*argv[]) {        //Binding Simple Functions    intLen =2; Thread (printing, Len);//The default constructor is the copy value pass, and if you want to use reference passing, use the//thread (printing, ref (LEN));     while(EXIT1);//changes the state of the system block after timing in the thread;printf"DDD");    GetChar (); return 0;}
View Code


2: Using the Bind method

1#include <string>2#include <boost\thread.hpp>3#include <boost/asio.hpp>4#include <iostream>5 using namespaceBoost::asio;6 using namespaceboost;7 intExit1 =1;8 voidPrintingint& Len,Const Char*str)9 {        Tenprintf"%s", str); OneThis_thread::sleep_for (Chrono::seconds (len));//change System state after blocking timer in thread Alen++; -  -Exit1 =0; the } -  - int_tmain (intARGC, _tchar*argv[]) - { +Thread (bind (printing,2,"thread runing"));//bind a function to a function object using the BIND function -      while(EXIT1);//changes the state of the system block after timing in the thread; +printf"DDD"); A GetChar (); at     return 0; -}
View Code

3: How to work with classes (core OH)

Note: (1) The first two is mainly the basic use of methods, and Method 3 has been used in the actual engineering development, Method 3 is mainly involved in a state transformation framework, in each state, there is a while dead loop of the network or communication port data read and write, when a condition is met, the state of the jump While the while is blocked while the thread timer is converted as a background state;

(2) The thread timer in this paper should be called a thread-delay device, which is mainly run in the background; the thread becomes a worker function or a program worker method; The member function of this class is called as the callback processing after the delay in the thread is completed;

(3) If a state is not updated, it is only necessary to open the thread again in the callback;

(4) Of course, it is recommended to bind the worker method using bind, otherwise you will set the working function as static method, and use reinterpret_cast to cast the pointer;

(5) Regarding reinterpret_cast, my understanding is that "this pointer parameter can be coerced into this", so as to achieve the purpose of the member function;

(6) The most critical of all operations using the Boost, the compilation tool used in this paper is VS2013;

/* Thread timers are used in classes, it is important to note that: (1) You must use the "Bind member function" method to pass to the thread of worker thread methods, otherwise it can not be compiled, the advantage of using this method is that the worker thread does not have to be a static function, (2) If you do not use Bind method, you must "Worker thread is set to static function", adding this pointer, using this method to compare this pointer to get more difficult to understand, and the worker function must be static function; */#include "stdafx.h" #include <string># Include <boost\thread.hpp> #include <boost/asio.hpp> #include <iostream>using namespace Boost::asio ; using namespace Boost;enum program_state{state1,state2,state3,state4};class Testclassa{public:testclassa () {state = State1;} Program_state state;void Timeoutprint (int& len, const char * str); void Timeoutcallbackprint (); void run (); void proc_s Tate1 (); void Proc_state2 (); void static Statictimeoutprint (int& len, const char * str,void* this);}; void Testclassa::timeoutprint (int& len, const char * str) {printf ("%s in\n", str); This_thread::sleep_for (Chrono:: Seconds (len));//Change the System state after blocking timer in the thread ("%s out\n", str);//Use the this pointer directly to invoke the callback function This->timeoutcallbackprint ();} void Testclassa::timeoutcallbackprint () {this->state = state2;printf ("timeoutcallbackprint\n");} void Testclassa::p roc_state1 () {//in the manner of binding member functions, note that when binding member functions, a placeholder is required to provide the class instance, reference, or pointer thread (BIND (&testclassa:: Timeoutprint,this, 5, "Thread 1")), while (this->state = = state1) {}}void testclassa::statictimeoutprint (int& len , const char * str,void* this) {printf ("%s in\n", str); This_thread::sleep_for (Chrono::seconds (len));// Change system state after blocking timer in thread printf ("%s out\n", str);//this pointers require casts to access member functions ((reinterpret_cast<testclassa*> (This))) Timeoutcallbackprint ();} void Testclassa::p roc_state2 () {//takes a static function as thread (Testclassa::statictimeoutprint, 5, "Thread 2", this) and while (this- >state = = state2) {}}void Testclassa::run () {while (1) {switch (this->state) {case state1:proc_state1 (); break;case State2:proc_state2 (); Break;}}} int _tmain (int argc, _tchar* argv[]) {Testclassa *TC = new Testclassa (); Tc->run (); return 0;}

Use boost thread timer as background thread to toggle the main Loop program State method Summary

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.