我喜欢本文中的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;


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

最佳答案

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


InternatThreadEntry不是db_c_hndlr的成员,因为db_c_hndlr声明InternalThreadEntry。这只是一个简单的错字:Internat!= Internal。
db_c_hndlr是抽象类,因为在InternalThreadEntry中声明的纯虚函数MyThreadClass没有参数。


virtual void InternalThreadEntry() = 0;

is not implemented by

void InternalThreadEntry(void *func)


db_c_hndlr中。看起来不需要void *func,因此我将通过删除参数来解决此问题。

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

关于c++ - 如何使用专用线程类MyThreadClass,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52463021/

10-13 02:20