简体   繁体   English

std :: thread的分段错误

[英]Segmentation fault at std::thread

typedef struct _ppm_struct* ppm_struct;
typedef unsigned char ppm_subpixel;

typedef ppm_subpixel (*ppm_pixel_func)(ppm_subpixel);
struct ppm_pixel {
   ppm_subpixel red;
   ppm_subpixel green;
   ppm_subpixel blue;
};

struct _ppm_struct {
   unsigned int width;
   unsigned int height;
   unsigned int max_color;
   ppm_pixel *firstpixel;
};

So I have this function call: 所以我有这个函数调用:

void pppm_color_pixel(ppm_struct p, ppm_struct out, ppm_pixel_func func, int threads) {
//ppm_pixel_func func2 = &inverse_pixel;
unsigned int w = p->width;
unsigned int h = p->height;
ppm_pixel *iterator = p->firstpixel;
std::thread th[threads];

long long total_pixels = w*h;
unsigned int count_for_thread = total_pixels/threads;

for(int j = 0; j<threads; ++j) {

   th[j] = std::thread(pppm_color_chunk_pixel,func, *iterator,count_for_thread);
   iterator = &iterator[count_for_thread];
}

for(int j = 0; j<threads; ++j) {
    th[j].join();
}

The next two function: funct is inverse subpixel 接下来的两个函数:funct是逆子像素

ppm_subpixel inverse_subpixel(ppm_subpixel subpixel) {
    subpixel = 255- subpixel;
    return subpixel;
}

void pppm_color_chunk_pixel(ppm_pixel_func func, ppm_pixel start_pixel, unsigned int count) {
    ppm_pixel *iterator = &start_pixel;
    for(unsigned int i = 0; i< count; ++i) {
        iterator[i].red = func(iterator[i].red);
        iterator[i].green = func(iterator[i].green);
        iterator[i].blue = func(iterator[i].blue);
    }
}

The main: 主要的:

pppm_color_pixel(p,out, &inverse_subpixel,pppm_get_max_cores());

The problem is that when I run this it shows me a segmentation fault. 问题是,当我运行此命令时,它显示了分段错误。 This happen on this line: 这发生在这一行:

iterator[i].green = func(iterator[i].green);

What I really don't understand is that i = 2 or more when this happens. 我真正不明白的是,当这种情况发生时, i = 2或大于2。 It doesn't crash from the first time. 从第一次开始就不会崩溃。 Even if I try to call only a single thread the result is the same. 即使我尝试仅调用一个线程,结果也是相同的。

pppm_color_chunk_pixel accepts a copy of ppm_pixel . pppm_color_chunk_pixel接受的副本ppm_pixel Since it is copied by value, passing *iterator to it will cause only one element to be copied. 由于它是按值复制的,因此将*iterator传递给它只会导致复制一个元素。 Iterating over anything after &start_pixel will fail. &start_pixel之后迭代任何内容都会失败。 Since std::thread doesn't pass objects by reference, you also need a reference-wrapper for this. 由于std::thread不会按引用传递对象,因此您还需要一个引用包装器。

void pppm_color_chunk_pixel(ppm_pixel_func func, ppm_pixel& start_pixel, unsigned int count) {
ppm_pixel* iterator = &start_pixel;
for (unsigned int i = 0; i < count; ++i) {
    iterator[i].red   = func(iterator[i].red);
    iterator[i].green = func(iterator[i].green);
    iterator[i].blue  = func(iterator[i].blue);
}

Calling it: 调用它:

for (int j = 0; j < threads; ++j) {
   th[j] = std::thread(pppm_color_chunk_pixel, func, std::ref(*iterator), count_for_thread);
   iterator = &iterator[count_for_thread];
}

I have not tested it, but I guess this is your problem. 我没有测试过,但是我想这是您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM