Skip to content
Snippets Groups Projects
ThreadClass.h 837 B
Newer Older
  • Learn to ignore specific revisions
  • benbergk's avatar
    benbergk committed
    //
    // Created by Ben Bergkamp on 1/31/18.
    //
    
    #ifndef EECS398_SEARCH_THREADCLASS_H
    #define EECS398_SEARCH_THREADCLASS_H
    
    vcday's avatar
    vcday committed
    
    
    benbergk's avatar
    benbergk committed
    #include <pthread.h>
    
    class ThreadClass
    
    vcday's avatar
    vcday committed
    	{
    
    benbergk's avatar
    benbergk committed
    public:
    
    vcday's avatar
    vcday committed
    	ThreadClass ( )
    		{ };
    
    	virtual ~ThreadClass ( )
    		{ };
    
    	//Returns true if thread was created successfully
    	bool StartThread ( )
    		{
    		return ( pthread_create( &thread, NULL, StaticFuncToRun, this ) == 0 );
    		}
    
    	//Blocks until thread finishes
    	void WaitForFinish ( )
    		{
    		pthread_join( thread, NULL );
    		}
    
    	void Die ( )
    		{
    		pthread_cancel( thread );
    		}
    
    benbergk's avatar
    benbergk committed
    
    protected:
    
    vcday's avatar
    vcday committed
    	//IMPLEMENT THIS METHOD IN YOUR SUB CLASS WITH CODE YOU WANT YOUR THREAD TO RUN
    	virtual void run ( ) = 0;
    
    benbergk's avatar
    benbergk committed
    
    private:
    
    vcday's avatar
    vcday committed
    	static void *StaticFuncToRun ( void *This )
    		{
    		( ( ThreadClass * ) This )->run( );
    		return nullptr;
    		}
    
    	pthread_t thread;
    	};
    
    benbergk's avatar
    benbergk committed
    
    
    #endif //EECS398_SEARCH_THREADCLASS_H