Skip to main content
gamedev.net
🔒 Locked

need help understanding threads

Started by redwingdw 4 replies 500+ views
Original Post
redwingdw
redwingdw
Hi in my mission to understand graphics and game programming in C++ I am getting to the point where I am going to have to learn all about threads, so because of this I have been reading a good article at http://www.linuxselfhelp.com/HOWTO/C++Programming-HOWTO-18.html which is explaining the basics ......It mentions that: I need to specify a entry point function for the thread We have to do a few tricks to wrap a C++ class around it because the entry function cannot be a normal member function of a class. it can be a static member function of a class "Static member functions do not have access to the this pointer of a C++ object. They can only access static data. " when the article surprises me is when it says Fortunately, there is way to do it. Thread entry point functions take a void * as a parameter so that the caller can typecast any data and pass in to the thread. This has me scratching my head a bit as it sounds like its passing data into a void function? I submit the code below but do not unbderstand the reasoning of how and why this would work? could someone explain what is happening in the below code and how c++ allows arguments to void functions ......Im trying to look closely at the way the argument was passed and figure out the logic behind it.
class Thread
{
   public:
      Thread();
      int Start(void * arg);
   protected:
      int Run(void * arg);
      static void * EntryPoint(void*);
      virtual void Setup();
      virtual void Execute(void*);
      void * Arg() const {return Arg_;}
      void Arg(void* a){Arg_ = a;}
   private:
      THREADID ThreadId_;
      void * Arg_;

};

Thread::Thread() {}

int Thread::Start(void * arg)
{
   Arg(arg); // store user data
   int code = thread_create(Thread::EntryPoint, this, & ThreadId_);
   return code;
}

int Thread::Run(void * arg)
{
   Setup();
   Execute( arg );
}

/*static */
void * Thread::EntryPoint(void * pthis)
{
   Thread * pt = (Thread*)pthis;
   pthis->Run( Arg() );
}

virtual void Thread::Setup()
{
        // Do any setup here
}

virtual void Thread::Execute(void* arg)
{
        // Your code goes here
}

Thanks :-)
Evil Steve
Evil Steve
The function isn't void. A void* is a pointer to an object of unknown type. In this case, the object is your Thread class.

thread_create() takes a user parameter which is passed to the entry point function. Because thread_create() doesn't need to force you to use a certain type for the parameter, it uses a void*. In your code, you pass 'this' as that parameter (Which is a Thread*), and then in the thread function you convert the void* back to a Thread*.

So from then on all the functions are referring to the Thread*, which is the thread class you passed to the function.
chollida1
chollida1
If you don't want to write a wrapper may I suggest boost threads?? We use them where we work and havent' looked back.

They have the nice property of being cross platform as well:)

As well the source code is quite easy to follow along so if your goal is to learn how to write a thread wrapper you should be able to learn from their design. I believe they wrap pthreads fo Linux.

Cheers
Chris
CheersChris
redwingdw
redwingdw
Thanks for the replys

void * Arg() const {return Arg_;}
void Arg(void* a){Arg_ = a;}
private:

void * Arg_;

I follow what both you have said and it has helped but now im wandering what arg() and arg_ are?

is arg() some function in the thread class thats being over ridden in our class? and what is void * Arg_;?

david
dmail
dmail
Interesting, I recently did something similar but using templates (yes I know I could have used boost:) )
MaulingMonkey
MaulingMonkey
Quote:
Original post by chollida1
If you don't want to write a wrapper may I suggest boost threads??


QFE

Even if you /want to/ write a wrapper, you may want to use it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.