Using boost thread and a non-static class function

62,406

Solution 1

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);

Solution 2

As others mentioned, when you want to call an object method in a new thread, you have to supply the address of that object. But you don't need to call boost::bind, you can use the overloaded boost::thread constructor like this:

GUIThread = new boost::thread(&Main::MainThreadFunc, GUIMain);

If the method is in the same class you use this to get the address of the current instance, e.g.:

t = new boost::thread(&myclass::compute, this);

If the method has parameters, you can specify them after the second argument, e.g.:

t = new boost::thread(&myclass::compute, this, p1, p2);

Solution 3

boost::bind is your friend (it can sometimes have a rough way of showing it though)!

use GUIThread = new boost::thread(boost::bind(&Main::MainThreadFunc, GUIMain));

and then make your MainThreadFunc a regular member. That means that you can use the instance variables directly like you would normally do.

Something like this:

class GUIMain {
public:
  GUIMain() : m_Member(42) {}

  void MainThreadFunc() {
    // use all members as you would normally do
    std::cout << m_Member << std::endl;
  }

private:
  int m_Member;
};

Solution 4

In cases like this it is useful to think of non-static member functions as free functions that take the this as first parameter, for example in your case void MainThreadFunc(Main* this).

boost::thread accepts a nullary functor, so you have to pass it a nullary functor which contains a reference to the instance GUIMain and calls GUIMain->MainThreadFunc which, seen as I explained above, would be something like MainThreadFunc(GUIMain).

Boost (and now also C++ with TR1) provides helpers to create such functors, namely boost::bind (or alternatively boost::lambda::bind). The expression boost::bind(f, arg1, arg2, ...) means "return a nullary functor which calls f(arg1, arg2, ...)".

That said, you can use the following expression to create the thread:

GUIThread = new boost::thread(boost::bind(&Main::MainThreadFunc, GUIMain))

Solution 5

If your object is a functor, i.e. has an operator(), you can pass an instance of it to boost::thread. The operator() does not need to be static. For example:

#include <boost/thread.hpp>

struct th {
    void operator()();
};

void th::operator()()
{
    for (;;) {
        // stuff
    }
}

int main()
{
    th t;
    boost::thread my_thread( t ); // takes a copy of t !
    my_thread.join(); // blocks
    return 0;
}
Share:
62,406

Related videos on Youtube

contrapsych
Author by

contrapsych

CS major at Kansas State University, class of 2016. I am mostly interested in low level languages, around the areas of C/C++, but I enjoy tinkering in anything, so I have messed with HTML and C#. I also have experience writing Android applications with Java. My interests include AI and parallelism. I just like to know how things work down to the nitty gritty.

Updated on July 09, 2022

Comments

  • contrapsych
    contrapsych almost 2 years

    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.

  • Max Beikirch
    Max Beikirch almost 11 years
    +1 The only answer that explains why we need to use boost::bind!
  • Max Beikirch
    Max Beikirch almost 11 years
    I just don't get what the functor returned by boost::bind looks like. According to your definition, it should call Main::MainThreadFunc(GUIMain), but MainThreadFunc does not take any arguments. How does the functor look like internally?
  • Tomáš Zato
    Tomáš Zato over 8 years
    Is new absolutely required? If I create the thread as normal local variable, will it not work?
  • Scott 混合理论
    Scott 混合理论 over 8 years
    what if i call boost::thread* thr = new boost::thread(boost::bind(&Foo::some_function, this)); twice??
  • Michael
    Michael almost 8 years
    Very comprehensive. Thanks.
  • maxschlepzig
    maxschlepzig over 6 years
    @Pavel, because the OP used new in his/her question. The question focuses on calling a method in a new thread; looking at the different ways how to construct a thread object isn't really relevant for this question.
  • Pavel P
    Pavel P over 6 years
    @maxschlepzig when OP's code clearly has a bug it's a good idea to let them know. With that code GUIThread always leaks. It might be a good idea to point out that destructor perhaps should join the thread as well.