代码之家  ›  专栏  ›  技术社区  ›  jdt141

作为C++类成员启动线程的最佳方法?

  •  16
  • jdt141  · 技术社区  · 17 年前

    我想知道 最好的 启动一个C++类成员的线程的方法?我自己的方法如下…

    5 回复  |  直到 17 年前
        1
  •  15
  •   shmuelp    17 年前

    我通常使用类的静态成员函数,并使用指向类的指针作为void*参数。然后,该函数可以执行线程处理,或者使用类引用调用另一个非静态成员函数。然后,该函数可以引用所有类成员,而无需使用笨拙的语法。

        2
  •  23
  •   ravenspoint    11 年前

    这可以通过使用Boost库来实现,如下所示:

    #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
    

    笔记:

    • 它使用普通的类成员函数。不需要添加额外的静态成员来混淆类接口。
    • 只需在启动线程的源文件中包含boost/thread.hpp。如果您只是从Boost开始,那么这个庞大而令人生畏的软件包的其余部分都可以忽略。

    在C++ 11中,您可以做同样的操作,但不需要

    // 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
    
        3
  •  11
  •   Adam Rosenfield    17 年前

    您必须使用void*参数引导它:

    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);
        4
  •  3
  •   Thanatopsis    17 年前

    我使用了上述三种方法。 当我在C++中首次使用线程时,我使用了 静态成员函数 然后 朋友功能 最后 增强图书馆 . 目前我更喜欢助推。在过去的几年里,我成了一个狂热的鼓吹者。

    Boost是C++,因为CPAN是Perl。:)

        5
  •  0
  •   Spille    16 年前

    Boost库提供了一种复制机制,有助于传输对象信息 到新的线程。在另一个boost示例中,boost::bind将使用指针进行复制,指针也只是被复制。因此,您必须注意对象的有效性,以防止指针悬空。如果您实现了operator()并提供了一个复制构造函数,并直接传递对象,那么您不必关心它。

    一个更好的解决方案,可以避免很多麻烦:

    #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
    

    另一个boost示例在堆上创建线程对象,尽管没有意义。