2014-10-09 119 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