C++ 使用来自同一个类的函数在类内创建线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17472827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create thread inside class with function from same class
提问by user3728501
I want to be able to define a class with some data members, and a function which has access to those data members, which are to be private.
我希望能够定义一个包含一些数据成员的类,以及一个可以访问这些数据成员的函数,这些数据成员是私有的。
I then want a public function, which creates some threads, which operate on the data members of the class. I am having some trouble getting my code to compile.
然后我想要一个公共函数,它创建一些线程,这些线程对类的数据成员进行操作。我在编译代码时遇到了一些麻烦。
Don't worry about mutex or data protection, this isn't going to be a problem, since this is just some example code for testing.
不要担心互斥或数据保护,这不会成为问题,因为这只是一些用于测试的示例代码。
class foo {
public:
void make_foo_func_threads();
private:
void foo_func();
char private_data;
std::vector<std::thread> some_threads;
}
void foo::foo_func() {
while(1) {
private_data = 'A';
}
}
void foo::make_foo_func_thread() {
for(...) some_threads.push_back(std::thread(foo_func));
for(...) some_threads.join();
}
The compiler is giving me the error:
编译器给了我错误:
'no matching call to std::thread::thread()'
'没有对 std::thread::thread() 的匹配调用'
Apparently there is 'no known conversion for argument 1 from <unresolved overloaded function type>
to void (foo::*&&)'
.
显然,参数 1 从<unresolved overloaded function type>
到'没有已知的转换void (foo::*&&)'
。
Erm, yeah, I have no idea what that means apart from the compiler is having trouble understanding how to resolve foo_func - I think.
嗯,是的,除了编译器无法理解如何解决 foo_func 之外,我不知道这意味着什么 - 我想。
How can I help the compiler understand what I am trying to do, so it won't bother me with any more errors. No doubt the code I have written is not legal, and if that is the case could someone explain why that is the case to me. Thanks!
我怎样才能帮助编译器理解我想要做的事情,这样它就不会再有任何错误了。毫无疑问,我编写的代码是不合法的,如果是这样的话,有人可以向我解释为什么会这样。谢谢!
回答by hmjd
foo_func
is a (non-static
) member function, and it needs an instance of foo
on which to operate. This instance must be provided to the thread constructor. If you refer to the std::thread::threadreference page it explains what code is executed in the new thread. The relevant point is that which refers to f
being a pointer to member function:
foo_func
是一个(非static
)成员函数,它需要一个实例foo
来操作。此实例必须提供给线程构造函数。如果您参考std::thread::thread参考页,它会解释在新线程中执行的代码。相关的一点是指的f
是指向成员函数的指针:
- If
f
is pointer to a member function of classT
, then it is called. The return value is ignored. Effectively, the following code is executed:
(t1.*f)(t2, ..., tN)
if the type oft1
is eitherT
, reference toT
or reference to type derived fromT
.((*t1).*f)(t2, ..., tN)
otherwise.
- 如果
f
是指向 class 成员函数的指针T
,则调用它。返回值被忽略。有效地,执行以下代码:
(t1.*f)(t2, ..., tN)
如果 的类型t1
是T
,则引用T
或引用派生自 的类型T
。((*t1).*f)(t2, ..., tN)
除此以外。
so it is clear that the instance is required.
所以很明显实例是必需的。
Change to:
改成:
for(...) some_threads.push_back(std::thread(&foo::foo_func, this));
#include <iostream>
#include <thread>
#include <vector>
class foo
{
public:
void make_foo_func_threads()
{
for (int i = 0; i < 5; ++i)
some_threads.push_back(std::thread(&foo::foo_func, this));
for (auto& t: some_threads) t.join();
}
private:
void foo_func() { std::cout << "Hello\n"; }
std::vector<std::thread> some_threads;
};
int main()
{
foo f;
f.make_foo_func_threads();
}