Issues with Multi-threaded Raytracing I'm having some issues with my multi-threaded ray tracing implementation. It seems to only render about half of the image. Here is the picture:


Here is the code:

int max = (nx * ny); //width * height
volatile std::atomic&lt;int&gt; count(0);

int cores = std::thread::hardware_concurrency();
cores -= 1;
vector&lt;future&lt;vector&lt;rgbpacket&gt;&gt;&gt; future_vector;
cout &lt;&lt; "Threads supported: " &lt;&lt; cores &lt;&lt; "\n";
while (cores--) {
    future_vector.emplace_back(
        std::async([=, &amp;world, &amp;cam, &amp;dim, &amp;orth, &amp;count] {
            vector&lt;rgbpacket&gt; stack;
            while (true) {

                int index = count++;

                if (index &gt;= max) {

                    return stack;
                } else {
                    int k = (index % nx); //mod width to get column
                    int j = (index / nx); //divide width to get row

                    rgbpacket curpx = render(j, k, dim, world, cam, orth);
                    curpx.k = (unsigned int)k;
                    curpx.j = (unsigned int)j;
                    stack.push_back(curpx);

                }
            }
        })
    );
}

//parse the image buffer
for (unsigned int i = 0; i &lt; future_vector.size(); i++) {
    vector&lt;rgbpacket&gt; stack = future_vector[i].get();
    for (unsigned int k = 0; k &lt; stack.size(); k++) {
        rgbpacket rgb_comp = stack.back();

        //Using the new compressed packet
        rgbaColor conv;
        conv.r = rgb_comp.r;
        conv.g = rgb_comp.g;
        conv.b = rgb_comp.b;

        hslaColor pix = rgb2hsl(conv);
        HSLAPixel newpixel(pix.h, pix.s, pix.l);

        HSLAPixel &amp;imgpx = img.getPixel(rgb_comp.k, rgb_comp.j);
        imgpx = newpixel;

        stack.pop_back();
    }
}


For some preface, I am calculating the pixel with respect to the position with the index variable. Once I calculate it, I store it (along with the position) in the vector. After I calculate all of the pixel values, I parse them into an image buffer and later print that image.

I've narrowed the issue (I think) to where I am calculating the rendered pixel and storing it in the vector. For some reason, it does not store all of the pixels in the vector and I am not sure why? Any help is appreciated. Thanks!
I fixed it, It was just a coding mistake, there was nothing wrong with the implementation. So the error was in fact in the 2nd for-loop when I was parsing the image buffer. I was reaching for the last pixel in the vector and then popping it off from the back of the stack. 

This means that in the for loop condition, the vector size will be changing but k will still be incrementing the same, so it will effectively only do half of the elements per vector. Because as K is incrementing stack.size() is decrementing so they meet in the middle and the loop is done.

To fix this, I just used index notation, so:

rgbpacket &amp;rgbpixel = stack[index];


and then I deleted the line where I was popping off from the stack.

Even though this is a stupid bug,
I hope this helps other people.