Jump to content
  • Advertisement
Sign in to follow this  
Tsumuji

One more doubt in thread.

This topic is 4255 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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".

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by discman1028
What is the question?


Make each object start it's own thread function, since this class will be inherited by a new one, and so, the this new class can implement different code, for diferent objects eventually instantiated.

Quote:
Original post by paulecoyoteIf you are going along the thought that each sprite should have it's own thread... don't go there. Honestly, it's a bad place.


Why?

Share this post


Link to post
Share on other sites
Quote:
Original post by Tsumuji
Quote:
Original post by discman1028
What is the question?


Make each object start it's own thread function, since this class will be inherited by a new one, and so, the this new class can implement different code, for diferent objects eventually instantiated.

This is still not a question ;) (they typically end with a question mark)

Quote:
Original post by Tsumuji
Quote:
Original post by paulecoyoteIf you are going along the thought that each sprite should have it's own thread... don't go there. Honestly, it's a bad place.


Why?

Imagine you can get 200 sprites on the screen... Having 200 thread will introduce a large overhead that will penalize you in the end.

Moreover, sprite rendering is not something that can be parralelized - you are limited by your use of a single resource - namely, the graphic driver, whose purpose is to serialize the rendering (as of today, I know little hardware that can render 200 sprites in parrallel).

Regards,

Share this post


Link to post
Share on other sites
I'm not trying to do a parallel rendering. I know the this serialization. What I want to implement in parallel is the method logic(). This one will process the IA, collision detection, etc. Any processing that can be done in the CPU.

What I want to know, is how I implement such kind of software ;)
Trying now common C++ lib, but getting the same problems hehehe. I now resumed my code to this:

#include <cc++/thread.h>
#include <iostream>
#include <vector>
using namespace std;
using namespace ost;

class sprite : public Thread{
private:
static int objscounter;
void run(void){
logic( );
}
protected:
virtual void logic( ){
std::cout << "sprite Logic!!." << std::endl;
}
virtual void render( ){
std::cout << "sprite Render!!." << std::endl;
}
public:
sprite(){
objscounter++;
//logic( );
}
~sprite(){}
};
int sprite::objscounter=0;

class custom_sprite : public sprite{
public:
sprite* st;
custom_sprite(){
st = new sprite();
st->start();
}
~custom_sprite(){}
void logic( ){
std::cout << "CTM Logic!!." << std::endl;
}
void render( ){
std::cout << "CTM Render!!." << std::endl;
}
};


int main( int argc, char *argv[] ){
std::vector<custom_sprite> Sprite(3);
for ( uint i=0; i<Sprite.size(); i++ )
Sprite.render();

sleep(2);

return 0;
}




The output:
CTM Render!!.
CTM Render!!.
CTM Render!!.
sprite Logic!!.

The method logic() is run from the base class, not the inherited, and, is run only once. Why this? I think I'm forgetting something from the C++ language....

Share this post


Link to post
Share on other sites
Surely you don't really mean to both inherit from Sprite, and then create one as a member as well?

You should be working with pointers in your vector if you want virtual functions to work properly, though I don't quite see why that would be a problem here.

Share this post


Link to post
Share on other sites
ok, let's go step by step. I think is better, to me pass the problems to you, and you understand correctly my problems that I'm having. If you disagree, tell me ;)
I'm putting a simpler class than before to get a easy view:


class spr : public Thread{
public:
spr(){ this->start(); }
~spr(){}
void run(){
logic();
}
void logic(){
std::cout << "TEST LOGIC()." << std::endl;
}
void render(){
std::cout << "TEST RENDER()." << std::endl;
}
};

-----

vector<spr> S2(4);
/* for ( uint i=0; i<S2.size(); i++ )
S2.start();*/
for ( uint i=0; i<S2.size(); i++ )
S2.render();
for ( uint i=0; i<S2.size(); i++ )
S2.join();



So my question in this first step: How do I set up the thread without calling the method start() from outside? (This code was commented)
If I put the starts to work, (uncommenting them) everything works fine. But I want some automatic thing, since the thread must be obligged to start after the object is instantiated. But in the way I do now, the thread does not start.

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
This is still not a question ;) (they typically end with a question mark)


I'm glad you agree, I thought I was going nuts..

Share this post


Link to post
Share on other sites
It looks like your Thread class doesn't obey the rule of three:
Quote:
Rule of Three
If a class requires at least one out of the copy-constructor, destructor and copy-assignment operator it generally needs all three.

When you create a vector< T > with an initial size it is filled with copies of either a given T object, if provided, or a default initialised T object if not. So the line vector<spr> S2(4); is creating a default initialised spr and then copy-constructing four spr instances from that default initialised spr instance. I suspect copy-construction is not implemented correctly and so those four copies all represent exactly the same thread, rather than four separate threads. That single thread then gets started four times.

Σnigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!