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

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

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

我想知道启动作为C ++类成员的pthread的最佳方法吗? 我自己的方法可以作为答案...


这可以通过使用boost库来简单地完成,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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中,您可以做同样的事情,但无需提高

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 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

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


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);

我使用了上面概述的三种方法。
当我第一次在c ++中使用线程时,我使用了静态成员函数,然后使用了朋友函数,最后是BOOST库。目前,我更喜欢BOOST。在过去的几年中,我已经成为BOOST的顽固派。

BOOST对C ++来说就像CPAN对Perl一样。 :)


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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
#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
};
1
2
3
4
5
6
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示例在堆上创建线程对象,尽管没有意义。


推荐阅读

    开机启动命令linux?

    开机启动命令linux?,软件,标准,环境,工作,设备,系统,名称,情况,电脑,位置,lin

    linux启动sh命令?

    linux启动sh命令?,工作,系统,代码,服务,脚本,文件,命令,目录,方法,权限,Linux

    linux启动信息命令?

    linux启动信息命令?,系统,信息,时间,命令,工作,检测,情况,设备,标准,管理,lin

    linux命令行自启动?

    linux命令行自启动?,服务,系统,脚本,设备,人员,工具,密码,最新,实时,代码,lin

    linux命令启动es?

    linux命令启动es?,地址,综合,数据,环境,工具,情况,办公,代码,市场,报告,我想

    linux启动加载命令行?

    linux启动加载命令行?,服务,系统,数字,首页,终端,环境,命令,方法,脚本,文件,

    linux启动项加命令行?

    linux启动项加命令行?,系统,工作,项目,地址,命令,首页,数字,管理,服务,信息,

    linux下sh启动命令?

    linux下sh启动命令?,服务,标准,文件,工具,软件,权限,命令,脚本,终端,目录,Lin

    服务启动linux命令?

    服务启动linux命令?,服务,密码,信息,系统,名称,命令,文件,用户,下面,服务器,l

    linux查询线程命令?

    linux查询线程命令?,系统,第一,线程,命令,进程,代码,分时,软件,选项,界面,如

    linux启动进命令行?

    linux启动进命令行?,系统,首页,密码,工具,终端,情况,电脑,数字,界面,命令,如

    linux启动蓝牙命令?

    linux启动蓝牙命令?,设备,手机,系统,蓝牙,电脑,管理,网络,密码,通讯,信息,Lin

    linux启动进去命令行?

    linux启动进去命令行?,系统,工具,首页,电脑,终端,材料,密码,命令,快捷键,窗

    linux线程查询命令?

    linux线程查询命令?,系统,第一,线程,命令,软件,名称,信息,进程,选项,方法,Lin

    linux启动显示命令行?

    linux启动显示命令行?,系统,密码,终端,状态,首页,情况,基础,电脑,信息,工具,l

    linux命令行启动登陆?

    linux命令行启动登陆?,密码,系统,电脑,地址,设计,代码,项目,连续,工具,庞大,L

    linux的u盘启动命令?

    linux的u盘启动命令?,系统,电脑,工具,信息,软件,网上,名称,工作,设备,通用,从

    linux防火墙命令启动?

    linux防火墙命令启动?,系统,状态,管理,密码,服务,工具,防火墙,网络,信息,软

    linux线程查询命令?

    linux线程查询命令?,系统,第一,线程,命令,软件,名称,信息,进程,选项,方法,Lin

    linux启动显示命令行?

    linux启动显示命令行?,系统,密码,终端,状态,首页,情况,基础,电脑,信息,工具,l