пытаюсь использовать потоки на наборе мандлебров в с ++, но это помечает ошибки, которые я не понимаю - PullRequest
0 голосов
/ 13 апреля 2020
void write_tga(const char *filename, ThreadArgs args)
{
    ofstream outfile(filename, ofstream::binary);

    uint8_t header[18] = {
        0, // no image ID
        0, // no colour map
        2, // uncompressed 24-bit image
        0, 0, 0, 0, 0, // empty colour map specification
        0, 0, // X origin
        0, 0, // Y origin
        WIDTH & 0xFF, (WIDTH >> 8) & 0xFF, // width
        HEIGHT & 0xFF, (HEIGHT >> 8) & 0xFF, // height
        24, // bits per pixel
        0, // image descriptor
    };
    outfile.write((const char *)header, 18);

    for (int y = 0; y < HEIGHT; ++y)
    {
        for (int x = 0; x < WIDTH; ++x)
        {
            uint8_t pixel[3] = {
                image[y][x] & 0xFF, // blue channel
                (image[y][x] >> 8) & 0xFF, // green channel
                (image[y][x] >> 16) & 0xFF, // red channel
            };
            outfile.write((const char *)pixel, 3);
        }
    }

    outfile.close();
    if (!outfile)
    {
        // An error has occurred at some point since we opened the file.
        cout << "Error writing to " << filename << endl;
        exit(1);
    }
}


// Render the Mandelbrot set into the image array.
// The parameters specify the region on the complex plane to plot.
void compute_mandelbrot(double left, double right, double top, double bottom, ThreadArgs args)
{
    for (int y = 0; y < HEIGHT; ++y)
    {
        for (int x = 0; x < WIDTH; ++x)
        {
            // Work out the point in the complex plane that
            // corresponds to this pixel in the output image.
            complex<double> c(left + (x * (right - left) / WIDTH),
                top + (y * (bottom - top) / HEIGHT));

            // Start off z at (0, 0).
            complex<double> z(0.0, 0.0);

            // Iterate z = z^2 + c until z moves more than 2 units
            // away from (0, 0), or we've iterated too many times.
            int iterations = 0;
            while (abs(z) < 2.0 && iterations < MAX_ITERATIONS)
            {
                z = (z * z) + c;

                ++iterations;
            }

            if (iterations == MAX_ITERATIONS)
            {
                // z didn't escape from the circle.
                // This point is in the Mandelbrot set.
                image[y][x] = 0x000000; // black
            }
            else
            {
                // z escaped within less than MAX_ITERATIONS
                // iterations. This point isn't in the set.
                image[y][x] = 0xFFFFFF; // white
            }
        }
    }
}

std::thread myThread;
std::thread myThread2;

    ThreadArgs args;

    myThread = std::thread(compute_mandelbrot, args);
    myThread2 = std::thread(write_tga, args);

    cout << "Please wait..." << endl;

    // Start timing
    the_clock::time_point start = the_clock::now();

    // This shows the whole set.

    myThread.join();
    //compute_mandelbrot(-2.0, 1.0, 1.125, -1.125);

    // Stop timing
    the_clock::time_point end = the_clock::now();

    // Compute the difference between the two times in milliseconds
    auto time_taken = duration_cast<milliseconds>(end - start).count();
    cout << "Computing the Mandelbrot set took " << time_taken << " ms." << endl;


    myThread2.join();
    //write_tga("output.tga");
    return 0;

прямо под каждым mythread.join - строка, которую он заменяет, если это помогает. помечены следующие ошибки:

'std :: invoke: не найдено подходящей перегруженной функции' файл: строка потока: 43

'не удалось специализировать шаблон функции' неизвестный тип std :: invoke (_callable &&, _ Types && ...) noexcept () '

код работает, когда я вынимаю потоки, так что не жалко, если это кажется глупым вопросом. я долго не изучал c ++

edit: исправил бит, в котором я дважды использовал mythread2.join, и добавил две функции, обрабатываемые по цепочке

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...