使用 boost 线程和非静态类函数

2021-12-24 00:00:00 function multithreading static c++ boost

所以我做了一些研究,发现你可以创建一个 boost::thread 对象,并通过使用this"和 boost::bind 等让它从一个非静态类函数开始.它真的没有对我来说很有意义,我能找到的所有示例都在与它开始的函数相同的类中启动了 boost::thread 对象,因此可以使用它.然而,我正在不同的类中启动线程,所以我担心使用this",我会说this"来自我创建线程的类,而不是函数所在的类(我可能错了,我需要更多地了解这个这个"家伙).这是我遇到问题的来源示例.

ANNGUI.h

<前>类ANNGUI{私人的:boost::thread *GUIThread;主 *GUIMain;上市://创建整个 GUI 和所有子部分.int CreateGUI();}

ANNGUI.cpp

<前>int ANNGUI::CreateGUI(){GUIMain = new Main();GUIThread = new boost::thread(GUIMain->MainThreadFunc);};

这不是全部的来源,但我认为我的问题出在某处,我知道我必须以某种方式处理这个",但我不确定如何处理.我可以使用静态函数,但我也不想将变量设为静态.谢谢.

另外,是否有使用任何 boost 库的非常好的资源?他们的网站文档看起来不错,但我无法理解.

解决方案

this 关键字与 boost::bind 一起使用,当您创建的函数对象是绑定到对象成员函数.成员函数不能脱离实例而存在,因此当使用 boost::bind 从成员函数中创建函子对象时,您需要一个指向实例的指针.这正是 this 关键字的实际含义.如果您在类的成员函数中使用 this 关键字,您将获得一个指向该类当前实例的指针.

如果你要从一个类成员函数的外部调用bind,你可能会说:

int main(){福 f;boost::thread* thr = new boost::thread(boost::bind(&Foo::some_function, &f));}

这里,我们使用 Foo::some_function 作为我们的线程函数.但是我们不能使用this,因为我们从main 调用bind.但是,如果我们从 Foo 的成员函数中调用 bind ,则可以使用 this 实现同样的事情,如下所示:

void Foo::func1(){boost::thread* thr = new boost::thread(boost::bind(&Foo::some_function, this));}

如果成员函数是静态的,或者只是一个普通(非成员)函数,那么你根本不需要实例指针.你会这样做:

boost::thread* thr = new boost::thread(some_regular_function);

So I have done some research, and have found you can create a boost::thread object and have it start with a non-static class function by using "this" and boost::bind etc. It really doesn't make much sense to me and all the examples I could find had the boost::thread object launched within the same class as the function it was starting with so this could be used. I however, am launching the thread in a different class so I'm afraid by using "this", I will be saying the "this" is from the class I am creating the thread from, rather than the one the function is in (I'm probably wrong, I need to learn more about this "this" guy). Here is an example of my source I am having the problem with.

ANNGUI.h

class ANNGUI
{
private:
    boost::thread *GUIThread;
    Main *GUIMain;
public:
    // Creates the entire GUI and all sub-parts.
    int CreateGUI();
}

ANNGUI.cpp

int ANNGUI::CreateGUI()
{
        GUIMain = new Main();
    GUIThread = new boost::thread(GUIMain->MainThreadFunc);
};

This isn't all the source, but I think my problem is in here somewhere, I know I have to deal with the "this" somehow, but I'm unsure how. I Could use a static function, but I didn't really want to make my variables static either. Thanks.

Also, Is there any very good resource for using any boost libraries? Their web site documentation seems good, but over my head.

解决方案

The this keyword is used with boost::bind when the function object you're creating is bound to a object member function. Member functions can't exist apart from instances, so when creating a functor object out of a member function with boost::bind, you need a pointer to an instance. That's exactly what the this keyword actually is. If you use the this keyword within a member function of a class, what you get is a pointer to the current instance of that class.

If you were to call bind from outside a class member function, you might say something like:

int main()
{
  Foo f;
  boost::thread* thr = new boost::thread(boost::bind(&Foo::some_function, &f));
}

Here, we're using Foo::some_function as our thread function. But we can't use this because we're calling bind from main. But the same thing could be achieved using this if we called bind from within a member function of Foo, like so:

void Foo::func1()
{
  boost::thread* thr = new boost::thread(boost::bind(&Foo::some_function, this));
}

If a member function is static, or is simply a regular (non-member) function, then you don't need an instance pointer at all. You would just do:

boost::thread* thr = new boost::thread(some_regular_function);

相关文章