如何解决主题:非标准语法
(我在 SO 上看过其他帖子也有同样的错误,但我检查了对我不起作用的推荐修复..)
我使用的是 Windows 10 和 Microsoft Visual Studio。
我正在尝试自学多线程,并且正在研究一个非常简单的多线程队列。
我创建了一个队列类,它有一个大小为 50 的数组,长度和计数都从 1 开始。
我有两个成员函数:一个在数组末尾添加另一个数字,另一个打印数组的第一个元素,然后通过将所有元素拉回一个索引来删除它(类似于常规队列)。
我的问题是线程。我希望两个函数同时运行,就好像整数是客户而数组是超市中的队列一样。顾客随机排队,只要队列不是空的,收银员就会叫下一个人过来。
这是我的代码: (您可以忽略 chrono 的包含,我计划通过在程序运行后每隔几秒添加或删除数字来使其更高级)
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
class Queue {
public:
Queue() {
length = 0;
count = 0;
}
void add() {
int x = 0;
while (x < 100) {
line[length] = count;
++length;
++count;
++x;
}
}
void read() {
int y = 0;
while (y < 100) {
if (length == 0) {
printf("Queue empty\n");
continue;
}
printf("Processed %d\n",line[0]);
for (unsigned int i = 0; i < length - 1; ++i) {
line[i] = line[i + 1];
}
--length;
++y;
}
}
private:
static const unsigned int MAX_SIZE = 50;
unsigned int line[MAX_SIZE];
unsigned int length;
unsigned int count;
};
int main()
{
Queue test;
thread a(test.add);
thread b(test.read);
return 0;
}
这是我的错误:
'Queue::add': non-standard Syntax; use '&' to create a point to member
'Queue::read': non-standard Syntax; use '&' to create a point to member
这些发生在主函数的两条线程行中。
我尝试在线程调用中添加 (),如:thread a(test.add())
而不是 thread a(test.add)
但这导致了错误:
no instance of constructor "std::thread::thread" matches the argument list
有谁知道我如何解决这个问题?
提前致谢:D
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。