#include <iostream>
#include <pthread.h>
class Core;
class Thread
{
public:
Thread();
virtual ~Thread();
protected:
virtual void run() = 0;
private:
static void* threadWrapper(void* me);
private:
pthread_t mThread;
};
Thread::Thread()
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&mThread, &attr, threadWrapper, this);
pthread_attr_destroy(&attr);
}
void* Thread::threadWrapper(void* me)
{
static_cast<Thread*>(me)->run();
return NULL;
}
Thread::~Thread()
{
void* dummy;
pthread_join(mThread, &dummy);
}
class Q : public Thread //Event queue that can be timed
{
public:
Q(Core* pb, void (Core::*method)()):
m_pb(pb),m_method(method){}
void show()
{
(m_pb->*m_method)();
}
private:
virtual void run()
{
for(;;)
{
sleep(1);std::cout << "running..." << std::endl;
}
}
private:
Core* m_pb;
void (Core::*m_method)();
};
class Core //The exellent core class which can be named with framework that handles and solves many complicated things
{
public:
Core()
{
m_a = new Q(this, &Core::show);
}
void onSomeWorkDone() //Always be called by other
{
m_a->show();
}
private:
void show()
{
std::cout<<"Executing..."<<std::endl;
}
private:
Q* m_a;
};
int main()
{
Core* c = new Core;
//w->show();//Invalid, the Core::show() is private
c->onSomeWorkDone();
while(1)
{
sleep(20);
}
}
#include <pthread.h>
class Core;
class Thread
{
public:
Thread();
virtual ~Thread();
protected:
virtual void run() = 0;
private:
static void* threadWrapper(void* me);
private:
pthread_t mThread;
};
Thread::Thread()
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&mThread, &attr, threadWrapper, this);
pthread_attr_destroy(&attr);
}
void* Thread::threadWrapper(void* me)
{
static_cast<Thread*>(me)->run();
return NULL;
}
Thread::~Thread()
{
void* dummy;
pthread_join(mThread, &dummy);
}
class Q : public Thread //Event queue that can be timed
{
public:
Q(Core* pb, void (Core::*method)()):
m_pb(pb),m_method(method){}
void show()
{
(m_pb->*m_method)();
}
private:
virtual void run()
{
for(;;)
{
sleep(1);std::cout << "running..." << std::endl;
}
}
private:
Core* m_pb;
void (Core::*m_method)();
};
class Core //The exellent core class which can be named with framework that handles and solves many complicated things
{
public:
Core()
{
m_a = new Q(this, &Core::show);
}
void onSomeWorkDone() //Always be called by other
{
m_a->show();
}
private:
void show()
{
std::cout<<"Executing..."<<std::endl;
}
private:
Q* m_a;
};
int main()
{
Core* c = new Core;
//w->show();//Invalid, the Core::show() is private
c->onSomeWorkDone();
while(1)
{
sleep(20);
}
}