Jump to content

c++ thread class conundrum


wetelectric
 Share

Recommended Posts

Im designing a program in c++ and im wondering the best way to implement its threading aspecs. Ive narrowed it down to three choices:

1. using pThread (this is a c library so not sure how that would work. Shouldnt be to difficult to do). It's also POSIX... ummm?

2. Boost libs. - not sure whether this will port. Also gotta think about distribution. But it does look 'nice'.

void hello_world() {

  cout << "Hello world, I'm a thread!" << endl;

}



int main(int argc, char* argv[]) {

  // start a new thread that calls the "hello_world" function

  boost::thread my_thread(&hello_world);

  // wait for the thread to finish

  my_thread.join();

3. design my own thread class: This was taken from an old site. It's a bit basic but meh, opensource and that

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*){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

}

Basically i'd like to know your experiences of implementing threads in c++, what worked for you?

cheers

Link to comment
Share on other sites

I personally like the cleanliness of POSIX threads. Very standard stuff, and I once wrote a little computing client program that would compile and run on a pretty diverse set of systems (mac, HPPA, Sparc, the various x86 OSes) without code changes or #ifdefs in it.

I mean, look at it. What could be simpler than:

#include <sys/types.h>

#include <unistd.h>

...

pid_t pid = fork();

if (pid < 0) {

    // Error

} else if (pid > 0) {

    // Parent. pid = child pid.

} else {

    // Child.

}

Link to comment
Share on other sites

mmm pthread does seem to be the one most people use.... but im worried about mixing C stuff with c++ code. Because one has to use malloc/free for the c and new/delete for c++. It can get to be a headache. To be honest i am leaning towards using the Boost libs. A lot of them are going to be included in the c++ update (yes in the next 100yrs c++ will have standard regExp support! ) anyways.... although ive heard some threading gurus are not liking the boost thread implementation.

mmm i'll be writing the code in such a way that it is easy to implement both and see which ones best anyways... i'll give the svn thing a test in a month or so then u can see

thanks guys

Link to comment
Share on other sites

Definitely go for pthreads, they can do everything a thread should be able to do and mixing c and c++ isn't that uncommon or horrible. Except of course if you are doing something inside windows where things get a lot more complicated. Just make sure to properly wrap away all the nasty c functions inside your class and do it in a nice object oriented fashion. Personally I like to mimic the thread class from Java in functionality and how you use it.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...