views:

7501

answers:

6

Can someone post a simple example of starting two (Object Oriented) threads in C++.

I'm looking for actual C++ thread objects that I can extend run methods on (or something similar) as opposed to calling a C-style thread library.

Thanks.

Update - I left out any OS specific requests in the hopes that whoever replied would reply with cross platform libraries to use. I'm just making that explicit now.

+2  A: 

Hi Zak, I found a useful-looking tutorial here:

http://www.linuxdocs.org/HOWTOs/C++Programming-HOWTO-24.html

Hope this helps!

Adam

Adam Alexander
+1  A: 

It largely depends on the library you decide to use. For instance, if you use the wxWidgets library, the creation of a thread would look like this:

class RThread : public wxThread {

public:
 RThread()
  : wxThread(wxTHREAD_JOINABLE){
 }
private:
 RThread(const RThread &copy);

public:
 void *Entry(void){
  //Do...

  return 0;
 }

};

wxThread *CreateThread() {
 //Create thread
 wxThread *_hThread = new RThread();

 //Start thread
 _hThread->Create();
 _hThread->Run();

 return _hThread;
}

If your main thread calls the CreateThread method, you'll create a new thread that will start executing the code in your "Entry" method. You'll have to keep a reference to the thread in most cases to join or stop it. More info here: wxThread documentation

Lck
You forgot to delete the thread.Perhaps you meant to create a *detached* thread?
aib
Yup right, I extracted the code from some code I'm currently working on and of course the reference to the thread is stored somewhere in order to join, stop and delete it later on. Thanks. :)
Lck
+5  A: 

You didn't specify your platform, but in most cases, I'd recommend that you take a look at Boost.Thread, a hello-world example of which is available here.

Pukku
+4  A: 

This DDJ article has some examples of using Boost.Thread.

Ferruccio
+2  A: 

I'd recommend using the Boost::Thread library. It offers easy portable threading. If you want OS-specific threading calls, you'll need to specify the OS, as DavidK mentioned.

Head Geek
+8  A: 

Well, technically any such object will wind up being built over a C-style thread library because C++ only just specified a stock 'std::thread' model in c++0x, which was just nailed down and hasn't yet been implemented. The problem is somewhat systemic, technically the existing c++ memory model isn't strict enough to allow for well defined semantics for all of the 'happens before' cases. Hans Boehm wrote an paper on the topic a while back and was instrumental in hammering out the c++0x standard on the topic.

http://www.hpl.hp.com/techreports/2004/HPL-2004-209.html

That said there are several cross-platform thread C++ libraries that work just fine in practice. Intel thread building blocks contains a tbb::thread object that closely approximates the c++0x standard and Boost has a boost::thread library that does the same.

http://www.threadingbuildingblocks.org/

http://www.boost.org/doc/libs/1_37_0/doc/html/thread.html

Using boost::thread you'd get something like:

#include <boost/thread.hpp>

void task1() { 
    // do stuff
}

void task2() { 
    // do stuff
}

int main (int argc, char ** argv) {
    using namespace boost; 
    thread thread_1 = thread(task1);
    thread thread_2 = thread(task2);

    // do other stuff
    thread_2.join();
    thread_1.join();
    return 0;
}
Edward Kmett
Boost thread is great - my only problem was that you couldn't (when I last used it) actually access the native underlying thread handle as it was a private class member! There's a TON of stuff in win32 that you need the threadhandle for, so we tweaked it to make the handle public.
Orion Edwards
Another problem with boost::thread is that as I recall you don't have the freedom to set the stack size of the new thread -- a feature that is also lamentably not included in c++0x standard.
Edward Kmett
That code gives me a boost::noncopyable exception for this line: thread thread_1 = thread(task1); Maybe it's because I'm using an older version (1.32).