-------------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.";
}
---------------------------------------------------------------
I am getting segmentation fault at : common/thread.cc:36 ( "this" is not able to >access function overridden in derived class). Please help me on this. I know something >very basic is wrong. If i write both the classes in same file or namespace it works.
This line is a problem.
void *(*threadRun) (void*) = reinterpret_cast <void *(*) (void *)> (CThread::ThreadRun) ;
You are taking a function that is supposed to return an int
and casting it to a function that returns a void
. The stack structure for those function types are most likely different.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.