I'm trying now to implement the code that I writed yesterday in the other topic to use that class in a object that will use this type of thread usage.
The class that implement the thread was:
#include <pthread.h>
struct Runnable {
virtual void run() = 0;
};
class Thread {
public:
Thread( Runnable *ptr ) {
_threadObj = ptr;
}
void start() {
pthread_t threadID;
pthread_create( &threadID, NULL, Thread::threadProc, _threadObj );
}
protected:
Runnable *_threadObj;
static void* threadProc(void* ptr){
(( Runnable* )ptr)->run();
return 0;
}
};
ok, now I want to use this in a sprite class, to start a method of this class in a thread. I'm tryinbg something like this:
class MyObject : public Runnable {
public:
virtual void run(){
std::cout << "Thread complete. " << std::endl;
}
};
class spriteIII{
private:
Thread *thread;
MyObject *obj;
public:
spriteIII( ){
obj = new MyObject( );
thread = new Thread( obj );
thread->start();
}
void logic( ){
std::cout << "Logic!!." << std::endl;
}
void render(){
std::cout << "Render!!." << std::endl;
}
};
........
std::vector<spriteIII> ts(3);
//ts.push_back( ts[0] );
for ( int i=0; i<ts.size(); i++ )
ts.render();
But what is the output? :
Render!!.
Render!!.
Render!!.
Thread complete.
As you can see, the method void run(), is done only once. Ok, it's because it's static, but point is: "Get a method in sprite class, that can be run in a thread, independently from one object to another".