创建一个以存储线程并调用它们的类
Creating a class to store threads and calling them
这是我要做的事情的简化版本:
#include <iostream>
#include <vector>
#include <thread>
#include <atomic>
class client {
private:
std::vector<std::thread> threads;
std::atomic<bool> running;
void main() {
while(running) {
std::cout << "main" << std::endl;
}
}
void render() {
while(running) {
std::cout << "render" << std::endl;
}
}
public:
client() {
running = true;
threads.push_back(std::thread(&client::main, this));
threads.push_back(std::thread(&client::render, this));
}
~client() {
running = false;
for(auto& th : threads) th.join();
};
};
int main() {
client c;
std::string inputString;
getline(std::cin, inputString);
return 0;
}
(注意:自写了问题以来已更改代码)
我要做的是创建一个为主循环(类),渲染和其他几件事的线程的类。但是,我无法使这个简化的版本起作用。我尝试使用Mutex锁定并解锁线程,但似乎没有任何帮助。我不知道为什么它不起作用,但我怀疑这是在threads.push_back(std::thread(this->main, this));
中使用this
的结果。
代码的当前结构不必保留...唯一的要求是将其自己的成员函数之一用作线程(并且该线程存储在类中)。我不确定这是否需要两个课程,或者我在一个班级中尝试这样做的方法是正确的方法。我已经看到了许多创建对象的示例,然后调用创建线程的成员。我试图避免这种情况,而是在构造函数中创建线程。
这里的问题是您不等待线程结束。在main
中,您创建c
。然后,这产生了线程。下一件事是返回摧毁c
的返回。当c
被摧毁时,它会破坏其成员。现在,如果线程尚未连接或分离,则将其销毁时,将调用std::terminate
,并且程序结束
您需要做的是在破坏者中,将running
设置为false
,然后在两个线程上调用join
。这将停止每个线程中的循环,并允许c
正确破坏。
这样做,但是提出了另一个问题。running
不是原子变量,因此在读取线程时写入它是未定义的行为。我们可以通过更改运行到提供同步的std::atomic<bool>
来验证。
我还必须更改线程结构。当您要使用成员函数时,语法应为
std::thread(&class_name::function_name, pointer_to_instance_of_class_name, function_parameters)
因此,在这种情况下,它将是
threads.push_back(std::thread(&client::main, this));
threads.push_back(std::thread(&client::render, this));
相关文章:
- 构造函数正在调用一个使用当前类类型的函数
- C++17 如何保存泛型可调用对象以供以后使用
- 是否可以定义一个以向量<T>作为值的unordered_map?
- C++ 如何根据作为输入传递的参数调用一个构造函数或另一个构造函数?
- 使用另一个函数调用一个函数(都在类中)时出现问题.没有错误代码C++
- 有没有一种 STL 方法可以找到字符串的所有排列,给出一个以 C++ 为单位的大小?
- 我是 C++ 的新手,我试图调用 make 一个以 2 个类作为其参数的类构造函数
- 为什么我可以调用一个从const方法更改成员的方法
- 我怎么能有一个以2D数组为参数的函数,而数组有一个我想更改的参数/维度
- 如何在另一个.cpp文件中调用一个.cpp文件中的函数
- 调用一个小函数两次(例如在if条件和主体中)比将结果存储在局部变量中更可取
- 使用线程从另一个成员函数调用一个Member函数
- 如何从perf_event_open的环形缓冲区中检索调用链以进行PERF_RECORD_SWITCH?
- 如何在另一个类中调用一个类的构造函数?
- 从 make 调用 CMake 以获取系统库路径
- llvm 调用一个以 char * 作为参数的外部函数
- 创建一个以存储线程并调用它们的类
- 包装一个使用char**[in/out]的C函数调用,以在cython中返回一个python列表
- 如何从文件中的类调用,以使用着色器显示在另一个包含gl代码的文件中(其中两个文件都在jni文件夹中)
- C++在main中调用一个以向量为参数的函数