如何使用专用线程类MyThreadClass

时间:2018-09-23 04:37:22

标签: c++ class polymorphism pthreads

我很喜欢本文中的MyThreadClasspthread function from a class,并希望在课堂上使用它,但我一直在努力弄清楚,到目前为止,我的是:

#include <pthread.h>
#include <iostream>
#include <vector>
#define OK      0
#define ERROR   -1

//-- ThreadClass
class MyThreadClass
{
public:
   MyThreadClass() {/* empty */}
   virtual ~MyThreadClass() {/* empty */}

   /** Returns true if the thread was successfully started, false if there was an error starting the thread */
   bool StartInternalThread()
   {
      return (pthread_create(&_thread, NULL, InternalThreadEntryFunc, this) == 0);
   }

   /** Will not return until the internal thread has exited. */
   void WaitForInternalThreadToExit()
   {
      (void) pthread_join(_thread, NULL);
   }

protected:
   /** Implement this method in your subclass with the code you want your thread to run. */
   virtual void InternalThreadEntry() = 0;

private:
   static void * InternalThreadEntryFunc(void * This) {
       ((MyThreadClass *)This)->InternalThreadEntry(); return NULL;
       }

   pthread_t _thread;
};
//-- /ThreadClass
//--- DUMMY DECLARATIONS BELOW TO MAKE IT COMPILE ---//
#define LOG_NS_ERROR std::cout
class test{
    public:
        int get_child(std::string x){return OK;};
};
test *_global;
typedef struct test_struct{} _db_transact;
class db_transact{
    public: 
        db_transact(int,int&,int&){};
};
int _ns;
int _log_id;
//--- DUMMY DECLARATIONS ABOVE TO MAKE IT COMPILE ---//
class db_c_hndlr : public MyThreadClass{
    public: 
        db_c_hndlr(void);
        ~db_c_hndlr(void);
        db_transact *db_conn_get(void);
        void InternalThreadEntry(void *func);
    private:
        int _stop;
        std::vector<db_transact*> _db_pool;
};
//---------------------------------------------------------

db_c_hndlr::db_c_hndlr(void) {
}
//---------------------------------------------------------

void db_c_hndlr::InternatThreadEntry(void *func) {

    while(!stop){
        std::cout << "going!" << std::endl;
        sleep(1);
    }
}
//---------------------------------------------------------

db_c_hndlr::~db_c_hndlr() {
    int i = 0;
    std::vector<db_transact*>::iterator it;
    for (i=0, it = _db_pool.begin();it!=_db_pool.end();it++, i++) {
        if (_db_pool[i])
            if (_db_pool[i]!=NULL) 
                delete _db_pool[i];
    }
}
//---------------------------------------------------------

db_transact *db_c_hndlr::db_conn_get(void) {
    db_transact *tmp;

    tmp = new db_transact(_global->get_child("db_config"), _ns, _log_id);
    _db_pool.push_back(tmp);
    return tmp;
}
//---------------------------------------------------------
int main(void)
{
    db_transact *conn=NULL;
    db_c_hndlr db;
    //db = new db_c_hndlr();

    conn= db.db_conn_get();
    return OK;
}

但是我得到的编译器消息如下:

$ g++ -lpthread pseudo_code.cpp 
pseudo_code.cpp:70:48: error: no ‘void db_c_hndlr::InternatThreadEntry(void*)’ member function declared in class ‘db_c_hndlr’
 void db_c_hndlr::InternatThreadEntry(void *func) {
                                                ^
pseudo_code.cpp: In function ‘int main()’:
pseudo_code.cpp:101:16: error: cannot declare variable ‘db’ to be of abstract type ‘db_c_hndlr’
     db_c_hndlr db;
                ^
pseudo_code.cpp:54:7: note:   because the following virtual functions are pure within ‘db_c_hndlr’:
 class db_c_hndlr : public MyThreadClass{
       ^
pseudo_code.cpp:29:17: note:    virtual void MyThreadClass::InternalThreadEntry()
    virtual void InternalThreadEntry() = 0;

,我正在努力弄清楚如何使它工作。

1 个答案:

答案 0 :(得分:3)

所提供的代码中有两个错误,导致报告了错误。

  1. InternatThreadEntry不是db_c_hndlr的成员,因为db_c_hndlr声明了InternalThreadEntry。这只是一个简单的错字:Interna t != Interna l

  2. db_c_hndlr是一个抽象类,因为在InternalThreadEntry中声明的纯虚函数MyThreadClass没有参数。

virtual void InternalThreadEntry() = 0;

不是由

实现的
void InternalThreadEntry(void *func)
db_c_hndlr

。似乎不需要void *func,因此我可以通过删除参数来解决此问题。

不相关:您可能会发现std::thread更通用,更便携。