2014-10-09 118 views
0
-------------common/cthread.h---------------- 
#include<iostream> 
using namespace std ; 
#include<string.h> 
#include<pthread.h> 
#include<stdio.h> 
#include<errno.h> 

namespace Framework 
{ 
class CThread 
{ 
public: 
CThread(void) 
{ 
} 
virtual ~CThread(void) 
{ 
} 
void Execute() ; 
virtual unsigned int Run(void) = 0 ; 
static unsigned int ThreadRun(void *obj) ; 

protected: 
pthread_t thread_obj ; 

private: 
int thread_ret_value ; 
}; 
} 
-----------common/cthread.cc---------------- 
#include "cthread.h" 

namespace Framework 
{ 

void *(*threadRun) (void*) = reinterpret_cast <void *(*) (void *)> (CThread::ThreadRun) ; 
//int CThread::value_part = 20 ; 

void CThread::Execute() 
{ 
    cout<<endl<<"["<<__FILE__<<":"<<__LINE__<<"] "<<"Initializing CThread..."; 
    try 
    { 
     thread_ret_value = pthread_create(&thread_obj, 0, threadRun, this) ; 
     if(thread_ret_value) 
     { 
      cout<<"["<<__FILE__<<":"<<__LINE__<<"] "<<"Thread could not be created : "<<strerror(thread_ret_value); 
      fprintf(stderr,"Error - pthread_create() return code: %d\n",thread_ret_value); 
     } 
     else 
      pthread_join(thread_obj, NULL) ; 
    } 
    catch(exception& e) 
    { 
     cout<<endl<<"["<<__FILE__<<":"<<__LINE__<<"] "<<"Exception Occured in ["<<__FILE__<<":"<<__LINE__<<"] "<<e.what() ; 
    } 
    cout<<endl<<"["<<__FILE__<<":"<<__LINE__<<"] "<<"Exiting CThread..."; 
} 

unsigned int CThread::ThreadRun(void *obj) 
{ 
    int ret_status = 0 ; 
    cout<<endl<<"["<<__FILE__<<":"<<__LINE__<<"] "<<"Started CThread..."; 
    CThread *pthread = (CThread*) obj ; 
    cout<<"\nRunning CThread..." ; 
    pthread->Run() ; 
    cout<<"\nStopped CThread..." ; 
} 

------------------dispatcherthread.h------------------------ 
#include<iostream> 
using namespace std ; 

#include "common/cthread.h" 
using namespace Framework ; 

class Dispatcherthread: public CThread 
{ 

    public: 
    Dispatcherthread() 
    { 
     cout<<"\nConstructor Dispatcherthread Called." ; 
    } 
    //static int nuThread ; 
    void Initialize() ; 
    virtual unsigned int Run() ; 
    void aman() ; 
    int *dispatcherImp(); 
    void stop() ; 
}; 
-------------------dispatcherthread.cc------------------- 
#include"dispatcherthread.h" 

void Dispatcherthread::Initialize() 
{ 
    cout<<endl<<"["<<__FILE__<<":"<<__LINE__<<"] "<<"Initializing Dispatcher Thread..."; 
} 

unsigned int Dispatcherthread::Run() 
{ 
    cout<<"\nThread started."; 
/// for(int i=0; i<=10; i++) 
///  cout<<"\nThread is running : "<<i ; 
    cout<<"\nThread stopped."; 
} 
--------------------------------------------------------------- 

我在以下位置出现segment fault:common/thread.cc:36(“this”无法访问函数在派生类中重写)。请帮助我。我知道一件事>非常基本的是错误的。如果我将这两个类写入同一个文件或命名空间,它就可以工作。在基类funciton中使用此派生类成员函数,其中派生类中的overriden函数

回答

0

这条线是一个问题。

void *(*threadRun) (void*) = reinterpret_cast <void *(*) (void *)> (CThread::ThreadRun) ; 

你正在服用的是应该返回int函数,它铸造一个返回void的功能。这些函数类型的堆栈结构很可能不同。

+0

在这种情况下,如果我注释掉你提到的pthread_create和threadRun指针行,并把this-> Run();在common/cthread.cc的第16行,它应该是正确的?但它仍然没有得到分段错误。从gdb我可以看到'p this'表示“无法访问内存位置”。 – 2014-10-09 20:21:42

+0

void CThread :: Execute() cout << endl <<“[”<< __ FILE __ <<“:”<< __ LINE __ <<“]”<<“初始化CThread ...”; 尝试 this-> Run();在“<< __ FILE __ <<”中发生异常。“__ FILE __”:“<< __ LINE __ <<”]“<<”异常发生在[“__ FILE __ <<” :“<< __ LINE __ <<”]“<< e.what(); } cout << endl <<“[”<< __ FILE __ <<“:”<< __ LINE __ <<“]”<<“退出CThread ...”; } – 2014-10-09 20:27:35