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.";
}
---------------------------------------------------------------
common/thread.cc : 36 ("this"는 함수에 액세스 할 수 없습니다. 파생 클래스에서 재정의). 이것 좀 도와주세요. 나는 아주 기본적인 것이 잘못되었다는 것을 안다. 같은 파일이나 네임 스페이스에서 두 클래스를 모두 작성하면 작동합니다.기본 클래스 funciton에서 이것을 사용하여 파생 클래스 멤버 함수를 처리하는 중, 파생 클래스에서 재정의 된 함수
당신이 언급 한 pthread_create와 threadRun 포인터 라인을 주석 처리하고 this-> Run(); common/cthread.cc의 16 번 줄에서 올바르게 작동합니까? 하지만 아직 세분화 오류가 발생하지는 않습니다. gdb에서 'p this'가 "메모리 위치에 액세스 할 수 없습니다"라는 메시지가 나타납니다. –
void CThread :: Execute() { cout << endl << "["<< "FILE __ <<": "<< __ LINE __ <<"] "<<"CThread 초기화 중 ... "; 시도하십시오 { this-> Run(); } catch (예외 및 e) { cout << endl << "["<< "파일 __ <<": "<< __ LINE __ <<" ""<< "예외가 발생했습니다 ["<< __ FILE __ << " : "<< __ LINE __ <<"] "<< e.what(); } cout << endl << "[" "<< __ 파일 __ <<": "<< __ LINE __ <<" ""<< "CThread 종료 중 ..."; } –