我创建了一个函数void top()
,通过为每个像素发送射线来渲染图像。我想把它放在线程上。我使用了#include <thread>
库,但是当我在main()
方法中声明线程时,它给了我两个错误:错误C2893无法专门化功能模板'unknown-type std :: invoke(_Callable &&,_ Types && ... )noexcept()和错误C2672'std :: invoke':没有匹配的重载函数。我认为可能是因为void top()
调用了非静态和静态方法。刚学习线程技术时,对什么地方有问题感到困惑。下面的所有功能都在主cpp文件中。
void top(int& samples_per_pixel, camera& cam, const color& background, hittable& world, const int& max_depth) {
for (float j = image_height - 1; j >= image_height/2; --j) {
for (int i = 0; i < image_width; ++i) {
color pixel_color(0, 0, 0);
for (int s = 0; s < samples_per_pixel; ++s) {
auto u = (i + random_double()) / (image_width - 1); //Random double is static. From other header file
auto v = (j + random_double()) / (image_height - 1);
ray r = cam.get_ray(u, v); //get_ray() is non-static and in camera class
pixel_color += ray_color(r, background, world, max_depth); //Ray_color defined above top() method in same main cpp file
pixel_color += color(0, 0, 0);
}
auto r = pixel_color.x();
auto g = pixel_color.y();
auto b = pixel_color.z();
auto scale = 1.0 / samples_per_pixel;
r = sqrt(scale * r);
g = sqrt(scale * g);
b = sqrt(scale * b);
int ir = static_cast<int>(256 * clamp(r, 0.0, 0.999));
int ig = static_cast<int>(256 * clamp(g, 0.0, 0.999));
int ib = static_cast<int>(256 * clamp(b, 0.0, 0.999));
pixels[index++] = ir; //pixels defined above top() class
pixels[index++] = ig;
pixels[index++] = ib;
}
}
}
...
int main(){
...
std::thread first(top,samples_per_pixel, cam, background, world, max_depth); //Two errors being called here
first.detach();
}