C++ 使用内部方法启动线程 class
C++ start thread with method of internal class
我有一个 class B,它包含一个内部 class A。class A 有一个方法需要访问 B 的一些成员,所以它有一个参数对外部 class 和另一个参数的引用。
现在我想在 B.
的另一个线程中启动这个方法
我知道这可能是这个问题的重复:Start thread with member function
但是我下面的最小示例没有编译。它说:
Error C2672 "std::invoke": No matching overloaded function found.
Error C2893 Failed to specialize function template "unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)".
这可能与引用传递有关,但使用 std::ref
对我一点帮助都没有。
B.h
#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();
}
无论是使用 lambda 版本还是将函数包装到 A 中都没有帮助。我错过了什么?
您正在尝试将 nullptr
传递给 B::B()
中的 std::thread
构造函数:
myTread = std::thread(nullptr);
重写它以使用成员初始值设定项列表并让 std::thread
成为默认构造它可能如下所示:
B::B() : member_b{10}, myTread{}, myA{new A(5)} {}
我有一个 class B,它包含一个内部 class A。class A 有一个方法需要访问 B 的一些成员,所以它有一个参数对外部 class 和另一个参数的引用。 现在我想在 B.
的另一个线程中启动这个方法我知道这可能是这个问题的重复:Start thread with member function 但是我下面的最小示例没有编译。它说:
Error C2672 "std::invoke": No matching overloaded function found.
Error C2893 Failed to specialize function template "unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)".
这可能与引用传递有关,但使用 std::ref
对我一点帮助都没有。
B.h
#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();
}
无论是使用 lambda 版本还是将函数包装到 A 中都没有帮助。我错过了什么?
您正在尝试将 nullptr
传递给 B::B()
中的 std::thread
构造函数:
myTread = std::thread(nullptr);
重写它以使用成员初始值设定项列表并让 std::thread
成为默认构造它可能如下所示:
B::B() : member_b{10}, myTread{}, myA{new A(5)} {}