C++11 Thread Class How to Use a Class Member Function

Start thread with member function

#include <thread>
#include <iostream>

class bar {
public:
void foo() {
std::cout << "hello from member function" << std::endl;
}
};

int main()
{
std::thread t(&bar::foo, bar());
t.join();
}

EDIT:
Accounting your edit, you have to do it like this:

  std::thread spawn() {
return std::thread(&blub::test, this);
}

UPDATE: I want to explain some more points, some of them have also been discussed in the comments.

The syntax described above is defined in terms of the INVOKE definition (§20.8.2.1):

Define INVOKE (f, t1, t2, ..., tN) as follows:

  • (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T and t1 is an object of type T or a reference to an object of
    type T or a reference to an object of a type derived from T;
  • ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a class T and t1 is not one of the types described in the previous
    item;
  • t1.*f when N == 1 and f is a pointer to member data of a class T and t 1 is an object of type T or a

    reference to an object of type T or a reference to an object of a

    type derived from T;
  • (*t1).*f when N == 1 and f is a pointer to member data of a class T and t 1 is not one of the types described in the previous item;
  • f(t1, t2, ..., tN) in all other cases.

Another general fact which I want to point out is that by default the thread constructor will copy all arguments passed to it. The reason for this is that the arguments may need to outlive the calling thread, copying the arguments guarantees that. Instead, if you want to really pass a reference, you can use a std::reference_wrapper created by std::ref.

std::thread (foo, std::ref(arg1));

By doing this, you are promising that you will take care of guaranteeing that the arguments will still exist when the thread operates on them.


Note that all the things mentioned above can also be applied to std::async and std::bind.

c++11 Thread class how to use a class member function

This isn't the right place for a reference wrapper. A simple pointer suffices, though, and achieves the desired result:

std::thread t1(&A::foo, &a, 100);

C++11 multithreading with class member function

You need to pass two things: a pointer-to-member, and the object. You cannot call a non-static member function (like Gamma) in C++ without an object. The correct syntax would be:

std::thread gamma_thread(&Beta::Gamma, // the pointer-to-member
my_beta, // the object, could also be a pointer
5); // the argument

You can think of my_beta here as being the first argument to Gamma(), and 5 as the second.

In C++, How a std::thread can call a member function without creating an object?

how td_1 and td_2 called the member function operator() of class H without an object of class H?

td_1 and td_2 does create objects of type H. Those objects are temporaries. Next, those supplied function object(which are temporaries in this case) are moved/copied into the storage belonging to the newly created thread of execution and invoked from there.

You can confirm this by adding a default constructor and move constructor inside class H as shown below:

#include<iostream>
#include<thread>

class H {
public:
void operator()(){
printf("This is H(), I take no argument\n");
}

void operator()(int x){
printf("This is H(), I received %d \n",x);
}
//default constructor
H()
{
std::cout<<"default constructor called"<<std::endl;
}
//move constructor
H(H&&)
{
std::cout<<"move constructor called"<<std::endl;
}

};

int main(){

int param = 0xD;

std::thread td_1 = std::thread(H());
std::thread td_2 = std::thread(H(),param);

td_1.join();
td_2.join();


return 0;
}

The output of the above program is:

default constructor called
move constructor called
move constructor called
default constructor called
move constructor called
move constructor called
This is H(), I take no argument
This is H(), I received 13

C++ 11 : Start thread with member function and this as parameter

Removing the templates and the pointers for simplicity, this is more or less what you would want:

class PipelineJob 
{
private:
std::thread thread_;
void execute(PipelineJob* object) { ..... }
public:
PipelineJob()
{
thread_ = std::thread(&PipelineJob::execute, this, this);
}
~PipelineJob() { thread_.join(); }
};

Note that this is passed two times to the std::thread constructor: once for the member function's implicit first parameter, the second for the visible parameter PipelineJob* object of the member function.

If your execute member function does not need an external PipelineJob pointer, then you would need something like

class PipelineJob 
{
private:
std::thread thread_;
void execute() { ..... }
public:
PipelineJob()
{
thread_ = std::thread(&PipelineJob::execute, this);
}
~PipelineJob() { thread_.join(); }
};

C++11: Start thread in object with member function and this

That's because the handle to std::thread is destroyed (goes out of scope) before it's either joined or detached.

I suggest:

class MyClass {
private:
std::thread thread;
void run();
public:
~MyClass();
void start();
}

and:

MyClass:~MyClass() {
if (thread.joinable())
thread.join();
}

void MyClass::run() {
//do stuff
}

void MyClass::start() {
thread = std::thread (&MyClass::run, this); //Move assignment
}

Constructing a std::thread from a class member function

You have two problems:

  1. The fist the compiler even tells you how to solve: You need to use the address-of operator & to get a pointer to a member function.

  2. The second problem is that non-static member functions needs an object to be called on, which becomes the this pointer in the function. When creating threads this is passed as the first argument (it's not actually passed to the function though).

So to solve both your problems, your initializer need to look like

updateThread(&updateWorldLoop, this) 

Start thread with derived class's member function

Pass std::ref(myInstance). Note that std::thread constructor will make a copy of the arguments passed to it (see here), and you can't copy a myAbstractClass.

(Then, all of this will still work because std::thread functionality is described in terms of std::invoke, which unwraps the std::reference_wrapper obtained by std::ref and calls the pointer to member function onto it. See here).

Starting a member function with arguments in a separate thread

Just add the arguments to the thread's constructor:

QString a("test");
std::thread thr(&MyClass::doStuff, this, a);

As your function accepts a reference you should use std::ref() like this:

MyClass::doStuff(QString& str) { /* ... */ }

// ...

QString a("test");
std::thread thr(&MyClass::doStuff, this, std::ref(a)); // wrap references


Related Topics



Leave a reply



Submit