1#include <iostream>2#include <thread>3 using namespacestd;4 5 classMyClass6 {7 Public:8 //Static is initialized only once9 Static intnum;Ten MyClass () One { Anum + =1; - } -~MyClass () the { -num-=1; - } - }; + 21//Static members do not belong to any one object, any object can access 22//For homogeneous object communication at intMyclass::num =3; - - classMythread: PublicThread - { - Public: - Static intcount; in - Public: to //overloading two constructors +Mythread (): Thread ()//Subclass Call Parent class constructor - { the * } $Template<typename t,typename ... Args>Panax NotoginsengMythread (T &&func, Args &&...args): Thread (Forward<t> (func), forward<args>(args) ...) - { the + } A }; the + intMythread::count =0; - $ voidGo () $ { - while(1) - { theMythread::count + =1; -cout << this_thread::get_id () <<" "<< Mythread::count <<Endl;WuyiThis_thread::sleep_for (Chrono::seconds (3)); the } - Wu } - About voidMain () $ { - //MyClass my1, My2, my3; - //cout << my1.num << Endl; - //cout << my2.num << Endl; A //cout << my3.num << Endl; + the mythread my1 (go); - mythread my2 (go); $ mythread my3 (go); theCin.Get(); the}
88. Static members of a class, and implementations of inheriting multithreaded classes