C STD ::线程无效的void表达式使用
C++ std::thread invalid use of void expression
我的线程程序有问题。我知道问题是什么,我只是不知道如何解决。我正在设置任意数量的线程来创建一个mandelbrot集,然后将其写入PPM文件。我正在使用std :: thread的向量,并调用mandelbrot类成员函数进行线程。问题发生在这里。我正在调用编译器不喜欢的void(void)函数。如何修复此问题,以使线程执行void(void)函数?我的代码如下:
main.cpp
int main(int argc, char **argv) {
const unsigned int WIDTH = 1366;
const unsigned int HEIGHT = 768;
int numThreads = 2;
Mandelbrot mandelbrot(WIDTH, HEIGHT);
if(argc > 1) {
numThreads = atoi(argv[1]);
}
std::vector<std::thread> threads;
for(int i = 0; i < numThreads; ++i) {
threads.emplace_back(mandelbrot.mandelbrotsetThreaded());
}
for(int i = 0; i < numThreads; ++i) {
threads[i].join();
}
return 0;
}
mandelbrot.cpp
void Mandelbrot::mandelbrotsetThreaded() {
while(true) {
int row = 0;
{
std::lock_guard<std::mutex> lock(row_mutex);
row = cur_row++;
}
if(row == width) return;
createMandelbrotSet(row);
}
}
threads.emplace_back(mandelbrot.mandelbrotsetThreaded());
// ^^
// note this!
那小线实际上将 call mandelbrot.mandelbrotsetThreaded()
,并尝试使用返回值传递给threads.emplace_back()
。当将返回类型指定为void
: - )
您想要的是函数(地址)本身,而不是功能的结果,例如:
threads.emplace_back(mandelbrot.mandelbrotsetThreaded);
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- (C++)分析树以计算返回错误值的简单算术表达式
- void() 是一个有效的C++表达式吗?
- 为什么新表达式可以正确生成指针类型,即使它应该返回 void*?
- 在c++std::函数的上下文中无效使用void表达式
- 无效地将 void 表达式与队列和 .pop() 一起使用
- 代数树表达式C++错误:未解析的外部符号"Public:_thiscall AlgebraicTreeExpression::~AlgebraicTreeExpression(void) ..."
- 使用多线程处理对象数组 - 无效使用 void 表达式错误
- 您可以在表达式中使用 void 返回函数吗?
- C STD ::线程无效的void表达式使用
- static_casting constexpr void* 的结果是常量表达式吗?
- 错误:无效使用了void表达式C++
- LAMBDA 错误:类型 'void' 的条件表达式是非法的
- lambda表达式中出现无效的使用Void表达式错误
- 错误:在类方法声明中无效地使用void表达式
- c++ 14是否要求delete表达式必须调用' void operator::delete(void*, std::
- 无法在VS 14 CTP中使用auto声明lambda:类型为'void'的条件表达式是非法的
- 无效地将void表达式与线程和对象一起使用
- 编译错误:无效使用了void表达式
- 可变模板:无效使用void表达式