A way to destroy “thread” class

前端 未结 8 1253
甜味超标
甜味超标 2021-02-06 18:22

Here is a skeleton of my thread class:

class MyThread {
public:
   virutal ~MyThread();

   // will start thread with svc() as thread entry point
   void start()         


        
8条回答
  •  再見小時候
    2021-02-06 18:43

    i give a simple and clean design, no signal, no sync, no kill needed.

    per your MyThread, i suggest renaming and adding as below:

    class MyThread { 
    public: 
       virutal ~MyThread(); 
    
       // will be called when starting a thread, 
       // could do some initial operations 
       virtual bool OnStart() = 0;  
    
       // will be called when stopping a thread, say calling join().
       virtual bool OnStop() = 0;
    
       // derive class will specialize what the thread should do, 
       // say the thread loop such as 
       // while (bRunning) {
       //    do the job.
       // } 
       virtual int OnRun() = 0;                 
    }; 
    

    the thread interface user will control the lifetime of MyThread.

    and actually the real thread object is as below:

        class IThread
        {
        public:
            virtual API ~IThread() {}
    
            /* The real destructor. */
            virtual void Destroy(void) = 0;
    
            /* Starts this thread, it will call MyThread::OnStart() 
                 * and then call MyThread::OnRun() just after created 
             *   the thread. */
            virtual bool Start(void) = 0;
    
            /* Stops a thread. will call MyThread::OnStop(). */
            virtual void Stop(void) = 0;
    
            /* If Wait() called, thread won't call MyThread::OnStop().
             * If could, it returns the value of MyThread::OnRun()
             *   returned */
            virtual int Wait(void) = 0;
    
            /* your staff */
            virtual MyThread * Command(void) = 0;
    
        };
    
    /* The interface to create a thread */
    extern IThread * ThrdCreate(MyThread *p);
    

    See the complete interfaces

    http://effoaddon.googlecode.com/svn/trunk/devel/effo/codebase/addons/thrd/include/thrd_i.h
    

    Coding Examples

    Case 1. Controlled thread loop

    class ThreadLoop : public MyThread
    {
    private:
       bool m_bRunning;
    public:
       virtual bool OnStart() { m_bRunning = true; }  
    
       virtual bool OnStop() { m_bRunning = false; }
    
       virtual int OnRun() 
       {
             while (m_bRunning) {
                  do your job;
             }
       }                 
    };
    
    int main(int argc, char **argv)
    {
          ThreadLoop oLoop;
    
          IThread *pThread = ThrdCreate(&oLoop);
          // Start the thread, it will call Loop::OnStart() 
          //and then call Loop::OnRun() internally.
          pThread->Start();
          do your things here. when it is time to stop the thread, call stop().
          // Stop the thread, it will call Loop::OnStop(), 
          // so Loop::OnRun() will go to the end
          pThread->Stop();
          // done, destroy the thread
          pThread->Destroy();
    }
    

    Case 2. Don't know when the thread will stop

    class ThreadLoop : public MyThread
    {
    public:
       virtual bool OnStart() {  }  
    
       virtual bool OnStop() { }
    
       virtual int OnRun() 
       {
             do your job until finish.
       }                 
    };
    
    int main(int argc, char **argv)
    {
          ThreadLoop oLoop;
    
          IThread *pThread = ThrdCreate(&oLoop);
          // Start the thread, it will call Loop::OnStart() 
          //and then call Loop::OnRun() internally.
          pThread->Start();
          do your things here. Since you don't know when the job will 
          finish in the thread loop. call wait().
          // Wait the thread, it doesn't call Loop::OnStop()
          pThread->Wait();
          // done, destroy the thread
          pThread->Destroy();
    }
    

    A complete IThread implementation:

    see

    http://effoaddon.googlecode.com/svn/trunk/devel/effo/codebase/addons/thrd/src/thrd/thrd.cpp
    

提交回复
热议问题