#include<iostream>#include<vector>#include<pthread.h>#include"destory_free_while.h"using namespacestd; Worker::worker () {shutdown=false; Num_thread=Ten;}voidWorker::start () {tids.resize (num_thread); for(inti =0; I <num_thread; ++i) {if(Pthread_create (&tids[i], NULL, sleeping, This) ==0) {cout<<"Thread Start complete"<<Endl; }}}worker::~Worker () {cout<<"begin Destory Instance"<<Endl; Shutdown=true; for(unsignedintI=0; I<tids.size (); i++) {pthread_join (tids[i], NULL); }}void* Worker::sleeping (void*Arg) {Worker* Work = (worker*) Arg; while(true) {cout<<"."<<Flush; Sleep (1); if(work->shutdown) {cout<<"destory instance, thread quit"<<Endl; Break; } }}/*vim:set ts=4 sw=4 sts=4 tw=100*/
#ifndef Destory_free_while_h#defineDestory_free_while_h#include<vector>#include<pthread.h>classWorker { Public: Worker (); ~Worker (); voidstart (); Public: Static void* Sleeping (void*Arg); Public: Std::vector<pthread_t>TIDs; intNum_thread; BOOLshutdown;};#endif //Destory_free_while_h
<iostream>"destory_free_while.h"usingnamespace STD; int Main () { worker worker; Worker.start (); Sleep (a); " Program Exit " << Endl;}
C + + BREAK while