如何在类C++中制作四个线程
How to make four threads in a class C++
我在一个类中创建四个线程时遇到问题,每个线程使用另一个成员函数打印出每个向量的内容。但是,当我制作线程时,我在这 4 行上收到错误no instance of constructor "std::thread::thread" matches the argument list
。我不知道如果我尝试为线程使用另一个成员函数,为什么它不起作用。会不会是因为他们在课堂上?我将如何解决这 4 个错误?
class PrintfourVectors
{
private:
vector<string> one;
vector<string> two;
vector<string> three;
vector<string> four;
public:
void printOne()
{
// do stuff
}
void printTwo()
{
// do stuff
}
void printThree()
{
// do stuff
}
void printFour()
{
// do stuff
}
void makeFourThreads()
{
thread threadone(printOne); // error here
thread threadtwo(printTwo); // error here
thread threadthree(printThree); // error here
thread threadfour(printFour); // error here
threadone.join();
threadtwo.join();
threadthree.join();
threadfour.join();
}
};
一个问题是你正在调用非静态成员函数,并且这些函数有一个"隐藏"的第一个参数,该参数成为函数中的this
指针。因此,在使用非静态成员函数创建线程时,需要将对象实例作为参数传递给线程函数。
喜欢
thread threadone(&PrintfourVectors::printOne, this);
// ^^^^
// Pass pointer to object instance as argument to the thread function
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 用于矢量处理的多个线程
- 如何在多个线程中创建 QSql数据库连接时防止名称冲突
- 如何声明由多个线程调用的 C++ DLL 的内部类,而无需导出类
- 在两个线程上读/写 64 位,无互斥/锁定/原子
- 自 Windows 10 20H1 以来,具有单独线程的多个窗口停止工作
- ASIO signal_set多个 IO 线程不可靠,具体取决于代码顺序?
- 将 10 个线程与原子布尔值同步
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- C++线程安全:如果只有一个线程可以写入非原子变量,但多个线程从中读取. 会遇到问题吗?
- C++:在多个线程中访问同一数组/向量的不同单元格是否会产生数据竞赛?
- Qt 线程两个参数
- 如何在类C++中制作四个线程
- 使用OpenMP在四核系统上使用4个线程来加速问题
- 具有四个队列的多线程生产者/消费者
- 我们是否需要每个线程多个io_service用于具有单个接受器的线程 boost::asio 服务器
- 函数中的四个线程
- C++11线程:多个线程正在等待一个条件变量
- 在一个简单的c++ 11四线程程序中,如果我注释标准输出打印行,我的两个消费者线程就不会返回
- 多线程两个功能使用openMP