Best way to start a thread as a member of a C ++ class?

I am wondering how to start pthread, which is a member of a C ++ class? My own approach follows the answer ...

+16
c ++ pthreads
Sep 17 '08 at 18:15
source share
5 answers

I usually use a static member function of a class and use a pointer to the class as the void * parameter. This function can either perform thread processing or call another non-stationary member function with a reference to the class. This function can refer to all members of a class without inconvenient syntax.

+15
Sep 17 '08 at 18:27
source share

This can simply be done using the boost library, for example:

#include <boost/thread.hpp> // define class to model or control a particular kind of widget class cWidget { public: void Run(); } // construct an instance of the widget modeller or controller cWidget theWidget; // start new thread by invoking method run on theWidget instance boost::thread* pThread = new boost::thread( &cWidget::Run, // pointer to member function to execute in thread &theWidget); // pointer to instance of class 

Notes:

  • The usual member function of the class is used here. There is no need to add additional static elements that confuse your class interface.
  • Just include boost / thread.hpp in the source file where you start the thread. If you are just starting out with a boost, everything else from this large and intimidating package can be ignored.

In C ++ 11, you can do the same, but without boost

 // define class to model or control a particular kind of widget class cWidget { public: void Run(); } // construct an instance of the widget modeller or controller cWidget theWidget; // start new thread by invoking method run on theWidget instance std::thread * pThread = new std::thread( &cWidget::Run, // pointer to member function to execute in thread &theWidget); // pointer to instance of class 
+23
Sep 17 '08 at 18:41
source share

You must load it using the void * parameter:

  class A
 {
   static void * StaticThreadProc (void * arg)
   {
     return reinterpret_cast <A *> (arg) -> ThreadProc ();
   }

   void * ThreadProc (void)
   {
     // do stuff
   }
 };

 ...

 pthread_t theThread;
 pthread_create (& theThread, NULL, & A :: StaticThreadProc, this); 
+11
Sep 17 '08 at 18:32
source share

I used the three methods described above. When I first used streams in C ++, I used static member functions, then friend functions, and finally the BOOST libraries. I currently prefer BOOST. Over the past few years, I have become a true BOOST fan.

BOOST is C ++ since CPAN is for Perl. :)

+3
Sep 17 '08 at 20:28
source share

The acceleration library provides a copy mechanism that helps transfer information about an object to a new topic. In another example, boost boost :: bind will be copied using a pointer, which is also just copied. Therefore, you will have to take care of the validity of your object to prevent the dangling pointer. If you implement the operator () and instead create a copy constructor and pass the object directly, you do not need to take care of it.

A nicer solution that prevents a lot of trouble:

 #include <boost/thread.hpp> class MyClass { public: MyClass(int i); MyClass(const MyClass& myClass); // Copy-Constructor void operator()() const; // entry point for the new thread virtual void doSomething(); // Now you can use virtual functions private: int i; // and also fields very easily }; 



 MyClass clazz(1); // Passing the object directly will create a copy internally // Now you don't have to worry about the validity of the clazz object above // after starting the other thread // The operator() will be executed for the new thread. boost::thread thread(clazz); // create the object on the stack 

Another example of boost creates a thread object on the heap, although that doesn't make sense.

0
May 11 '10 at 16:29
source share



All Articles