wetelectric Posted December 30, 2006 Share Posted December 30, 2006 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 Quote Link to comment Share on other sites More sharing options...
nickisgod1 Posted December 30, 2006 Share Posted December 30, 2006 pthreads all the way. i have used it with c++ and experienced no problems. besides why build your own. pthreads is posix, and will be available on most *nix os you need to compile on, and you are complying with standards Quote Link to comment Share on other sites More sharing options...
cooper Posted December 31, 2006 Share Posted December 31, 2006 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. } Quote Link to comment Share on other sites More sharing options...
wetelectric Posted January 1, 2007 Author Share Posted January 1, 2007 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 Quote Link to comment Share on other sites More sharing options...
jool Posted January 1, 2007 Share Posted January 1, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.