简体   繁体   中英

C++ start thread with method of internal class

I have a class B that contains an internal class A. The class A has a method that needs to access some members of B, so it has as one argumet a reference to the external class and one other argument. Now i would like to start this method in another thread from B.

I know this might be a duplicate of this question: Start thread with member function But my following minimal example isn't compiling. It sais:

Error C2672 "std::invoke": No matching overloaded function found.

Error C2893 Failed to specialize function template "unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)".

This might be related to the pass by reference, but using std::ref does not help me at all.

Bh

#include <thread>

class B
{
public:
    class A
    {
     public:
        int member_a;
    
        A(int member);
        void calc(B& b, int factor);
        std::thread thread_starting_wrapper(B& b, int factor);
    };

    int member_b;
    std::thread myTread;
    A* myA;

    B();
    std::thread start_thread();
    std::thread start_thread_wrapper();
};

B.cpp

#include "B.h"
#include <iostream>


B::B()
{
    member_b = 10;
    myTread = std::thread(nullptr);
    myA = new A(5);
}

B::A::A(int member)
{
    member_a = member;
}


void B::A::calc(B& b, int factor)
{
     std::cout << (b.member_b + member_a) * factor << std::endl;
}

std::thread B::start_thread()
{
    return std::thread(&B::A::calc, myA, std::ref(*this), 2);
    // return std::thread([this] { this->myA->calc(std::ref(*this), 2); } );
}

std::thread B::A::thread_starting_wrapper(B& b, int factor)
{
    return std::thread([&] {calc(std::ref(b), factor);});
}

std::thread B::start_thread_wrapper()
{
     return this->myA->thread_starting_wrapper(std::ref(*this), 2);
}

main.cpp

#include "B.h"

int main()
{
    B* b = new B();
    std::thread t = b->start_thread();
    //std::thread t2 = b->start_thread_wrapper();
    t.join();
}

Neither using the lambda version nor wrapping the function into A helps. What am I missing?

You are trying to pass nullptr to the std::thread constructor in B::B() :

myTread = std::thread(nullptr);

Rewritten it to use the member initializer list and to let std::thread be default constructed it could look like this:

B::B() : member_b{10}, myTread{}, myA{new A(5)} {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM