C++ (Qt)class ThreadBase : protected QThread{ Q_OBJECTpublic: explicit ThreadBase( QObject* = 0 ); bool isInitialized(); void launch(); ~ThreadBase(); protected: virtual bool initialize(); virtual void run(); virtual void execute() = 0; virtual void finalize(); bool initialized; bool finish;};
C++ (Qt)ThreadBase :: ThreadBase( QObject* parent ) : QThread( parent ){ initialized = false; finish = false;} ThreadBase :: ~ThreadBase(){ finish = true; quit(); while ( isRunning() ) { yieldCurrentThread(); }} bool ThreadBase :: isInitialized(){ return initialized;} bool ThreadBase :: initialize(){ return true;} void ThreadBase :: run(){ initialized = initialize(); if ( initialized ) { while( !finish ) { execute(); usleep( 1 ); } } else { finish = true; } finalize();} void ThreadBase :: finalize(){}
C++ (Qt)ThreadBase :: ~ThreadBase(){ finish = true; quit(); while ( isRunning() ) { yieldCurrentThread(); }}
C++ (Qt) while (execute()) usleep( 1 ); // а вместо этого лучше виртуальчик