• <small id='yIKKw'></small><noframes id='yIKKw'>

      <bdo id='yIKKw'></bdo><ul id='yIKKw'></ul>

      <legend id='yIKKw'><style id='yIKKw'><dir id='yIKKw'><q id='yIKKw'></q></dir></style></legend>
      <i id='yIKKw'><tr id='yIKKw'><dt id='yIKKw'><q id='yIKKw'><span id='yIKKw'><b id='yIKKw'><form id='yIKKw'><ins id='yIKKw'></ins><ul id='yIKKw'></ul><sub id='yIKKw'></sub></form><legend id='yIKKw'></legend><bdo id='yIKKw'><pre id='yIKKw'><center id='yIKKw'></center></pre></bdo></b><th id='yIKKw'></th></span></q></dt></tr></i><div id='yIKKw'><tfoot id='yIKKw'></tfoot><dl id='yIKKw'><fieldset id='yIKKw'></fieldset></dl></div>
      1. <tfoot id='yIKKw'></tfoot>
      2. 使用 boost 线程和非静态类函数

        时间:2023-06-29
          <bdo id='qK1RQ'></bdo><ul id='qK1RQ'></ul>

        • <tfoot id='qK1RQ'></tfoot>
          1. <i id='qK1RQ'><tr id='qK1RQ'><dt id='qK1RQ'><q id='qK1RQ'><span id='qK1RQ'><b id='qK1RQ'><form id='qK1RQ'><ins id='qK1RQ'></ins><ul id='qK1RQ'></ul><sub id='qK1RQ'></sub></form><legend id='qK1RQ'></legend><bdo id='qK1RQ'><pre id='qK1RQ'><center id='qK1RQ'></center></pre></bdo></b><th id='qK1RQ'></th></span></q></dt></tr></i><div id='qK1RQ'><tfoot id='qK1RQ'></tfoot><dl id='qK1RQ'><fieldset id='qK1RQ'></fieldset></dl></div>

            <small id='qK1RQ'></small><noframes id='qK1RQ'>

          2. <legend id='qK1RQ'><style id='qK1RQ'><dir id='qK1RQ'><q id='qK1RQ'></q></dir></style></legend>

                <tbody id='qK1RQ'></tbody>
                  本文介绍了使用 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);
                  

                  这篇关于使用 boost 线程和非静态类函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++ 函数回调:无法从成员函数转换为函数签名 下一篇:shared_ptr 在哪里?

                  相关文章

                  1. <i id='O3jcv'><tr id='O3jcv'><dt id='O3jcv'><q id='O3jcv'><span id='O3jcv'><b id='O3jcv'><form id='O3jcv'><ins id='O3jcv'></ins><ul id='O3jcv'></ul><sub id='O3jcv'></sub></form><legend id='O3jcv'></legend><bdo id='O3jcv'><pre id='O3jcv'><center id='O3jcv'></center></pre></bdo></b><th id='O3jcv'></th></span></q></dt></tr></i><div id='O3jcv'><tfoot id='O3jcv'></tfoot><dl id='O3jcv'><fieldset id='O3jcv'></fieldset></dl></div>

                    <small id='O3jcv'></small><noframes id='O3jcv'>

                  2. <legend id='O3jcv'><style id='O3jcv'><dir id='O3jcv'><q id='O3jcv'></q></dir></style></legend>

                        <bdo id='O3jcv'></bdo><ul id='O3jcv'></ul>
                      <tfoot id='O3jcv'></tfoot>