Thread of a member function

Daniel

Mainly for test purposes,

I want to run a member function on a thread.

Endless tries - and still, only error messages,

Please - can anyone explain the cause of the error and some best practices of doing so? Thanks

enter image description here

#include <thread>
#include <iostream>

using namespace std;

class Test {
public:
    int x = 1;
    int y = 7;
    int a() {
        for (int i = 1; i < 1000; i++) {
            x += y;
           cout << "x:" << x << endl;
        }
        return x;
    }
    int b() {
        for (int i = 1; i < 1000; i++) {
            y -= 0.5 * x;
            cout << "y:" << y << endl;
        }
        return y;
    }
    Test() {

    }
    Test* run() {
        thread(&Test::a, this);
        //thread(&Test::b, this);
        return this;
    }
};

int main()
{
    Test* obj = new Test();
    obj->run();
    return 0;
}
user17732522

Your std::thread object lives only until the end of the expression in which it is created as a temporary. When it is destroyed and the thread is still in a joinable state, std::terminate is called, which aborts your program.

You should store the std::thread object somewhere (e.g. in the Test object or in main or locally in run) and call .join() on it at the correct time where you expect the thread to finish (e.g. destructor or before the end of main or at the end of run).


Aside from that, if you run the out-commented thread as well, you have data races causing undefined behavior. You may not access non-atomic objects (such as x and y) in multiple threads without synchronization if at least one of these accesses is a write.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Start thread with member function

Starting thread with member function

Java - start thread with member function

Passing class's member function to std::thread

How to start a boost::thread running a member function?

Calling an objects member function in a thread in main()

c++ thread member function receive value

How to create a thread by a child member function

How do I run a member function on a thread?

Passing pointer to a member function to a seperate thread

THREAD ERROR: invalid use of non-static member function

Pass private variable to non-member function on separate thread

std::thread creation with class member function - best practice

Undesirable destructor call when starting a thread on a member function

thread member function and mex file crash with std::bad_alloc

Threading A Class Member Function; Thread Initialization Through Initializer List

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

Calling a class member function from a thread using pthread_create

Sleep affecting which virtual member function is called by std::thread?

Thread inside a class with member function from another class

Cannot compile with thread that uses member function within same class

Start std::thread in member function of object that does not have a copy constructor

How to call thread with a public member function from another class as argument

std thread call template member function of template class: compiler error

updated value of member variable is not reflacted within thread routin function in pthread

boost::thread invalid use of a non-static member function

compilation error when creating a thread with a member function with ref argument

Why does calling a member member function in a separate thread result in non-deterministic behaviour?

c++ 11 - passing member function to thread gives: no overloaded function takes 2 arguments