使用同一个类中的函数在类内部创建线程 [英] Create thread inside class with function from same class
问题描述
我希望能够定义一个包含一些数据成员的类,以及一个可以访问那些将是私有的数据成员的函数.
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();
}
编译器给我错误:
'没有匹配调用std :: thread :: thread()'
'no matching call to std::thread::thread()'
显然,没有已知的参数1从<unresolved overloaded function type>
转换为void (foo::*&&)'
的转换.
Apparently there is 'no known conversion for argument 1 from <unresolved overloaded function type>
to void (foo::*&&)'
.
Erm,是的,除了编译器难以理解如何解析foo_func之外,我不知道这意味着什么.
Erm, yeah, I have no idea what that means apart from the compiler is having trouble understanding how to resolve foo_func - I think.
我如何帮助编译器理解我要做什么,所以它不会再给我带来任何错误.毫无疑问,我编写的代码是不合法的,如果是这样的话,有人可以向我解释为什么.谢谢!
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!
推荐答案
foo_func
是(非static
)成员函数,它需要foo
的实例可以在其上进行操作.该实例必须提供给线程构造函数.如果您参考 std :: thread :: thread 参考页,解释了在新线程中执行什么代码.相关的一点是,它引用f
是成员函数的指针:
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::thread reference 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:
- 如果
f
是指向类T
的成员函数的指针,则将其调用.返回值将被忽略.有效地,执行以下代码:
(t1.*f)(t2, ..., tN)
,如果t1
的类型是T
,则引用T
或引用从T
派生的类型.((*t1).*f)(t2, ..., tN)
否则.
- 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.
因此很明显该实例是必需的.
so it is clear that the instance is required.
更改为:
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();
}
这篇关于使用同一个类中的函数在类内部创建线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!