how to write this code by using thread?

I am new in here and I need to write my code by threads! unfortunatly I couldnt find the way! it is the first time using threads. can any one help me please?

here is the code:
#include <iostream>
#include <fstream>
#include <complex>
#include <thread>
#include <mutex>
#include <algorithm>
#include <chrono>

using namespace std;

int max_color = 200;
int image_width = 1366, image_height = 768;
double view_x1, view_y1, view_x2, view_y2;

double getPointColor(double x, double y)
{
complex<double> c(x, y), z = 0;
int color = 0;
for (; color < max_color && norm(z) < (1 << 16); ++color)
z = z * z + c; // f(z) = z^2 + c
if (color < max_color)
return color + 1 - log(0.5 * log(norm(z)) / log(2)) / log(2);
else
return 0;
}

int getPixelColor(int row, int col)
{
int i, j;
double color = 0;
for (i = 0; i < 4; ++i)
{
double y = view_y1 + (view_y2 - view_y1) * (4 * row + i) / (4 * image_height);
for (j = 0; j < 4; ++j)
{
double x = view_x1 + (view_x2 - view_x1) * (4 * col + i) / (4 * image_width);
color += getPointColor(x, y);
}
}
// converting color to RGB
color = color * 7 / (16 * max_color);
int gLow = (int)floor(color), gHigh = gLow + 1;
color -= gLow;
int r = ((gLow & 4 ? 256 : 0) * (1 - color) + (gHigh & 4 ? 256 : 0) * color);
int g = ((gLow & 2 ? 256 : 0) * (1 - color) + (gHigh & 2 ? 256 : 0) * color);
int b = ((gLow & 1 ? 256 : 0) * (1 - color) + (gHigh & 1 ? 256 : 0) * color);
return min(255, r) << 16 | min(255, g) << 8 | min(255, b);
}

template<typename T>
void writeToBitmap(const char *filename, int width, int height, T getColor)
{
int imageSize = ((width + 1) * 3 & -4) * height;
ofstream file(filename, ios_base::binary);
file.write("BM", 2);
auto write = [&](int word, int size)
{
if ( size )
file.write((char *)&word, size);
};
write(54 + imageSize, 4);
write(0, 4);
write(54, 4);
write(40, 4);
write(width, 4);
write(height, 4);
write(1, 2);
write(24, 2);
write(0, 4);
write(imageSize, 4);
write(0, 4);
write(0, 4);
write(0, 4);
write(0, 4);
for (int row = height; --row >= 0;)
{
for (int col = 0; col < width; ++col)
write(getColor(row, col), 3);
write(0, 3 & -3 * width);
}
file.close();
}

int main()
{
int nthread;
cout << "Enter x1: ";
cin >> view_x1;
cout << "Enter y1: ";
cin >> view_y1;
cout << "Enter x2: ";
cin >> view_x2;
cout << "Enter y2: ";
cin >> view_y2;
cout << "Enter number of threads: ";
cin >> nthread;
int **bitmap = new int*[image_height];
for (int i = 0; i < image_height; ++i)
bitmap[i] = new int[image_width];
auto start_time = chrono::system_clock::now();

// TODO: Use threads to optimize this part of code

for (int row = 0; row < image_height; ++row)
for (int col = 0; col < image_width; ++col)
bitmap[row][col] = getPixelColor(row, col);

// -----------------------------------------------

auto finish_time = chrono::system_clock::now();
writeToBitmap("mandelbrot.bmp", image_width, image_height,
[&](int row, int col)
{
return bitmap[row][col];
});
for (int i = 0; i < image_height; ++i)
delete[] bitmap[i];
delete[] bitmap;
auto duration = finish_time.time_since_epoch() - start_time.time_since_epoch();
auto milliseconds = chrono::duration_cast<chrono::milliseconds>(duration);
cout << "Total Time: " << milliseconds.count() << "ms" << endl;
return 0;
}

//thanks so so so much
WTF!?!
First, please use code tags.

Do you have any idea as to how threads work? In your case, you will probably need a vector of threads (to fulfill your arbitrary number of threads requirement). Also, you can't return values from a thread. You have a few options here: The easiest would be to make your bitmap variable global, and then start threads, passing them the rows for them to deal with. Just make sure they don't have any overlaps, otherwise you could have some problems...

Here is a (poor) example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <vector>
#include <thread>
#include <algorithm>
#include <cmath>
// ...

int** bitmap;

// ...

void thread_main(int startRow, int numRows, int imgWidth) {
    for (int row = 0; row < numRows; ++row)
        for (int col = 0; col < imgWidth; ++col)
            bitmap[row+startRow][col] = getPixelColor(row+startRow, col);
}

int main() {
    std::vector<std::thread> threads;

    // ...

    int numRows = std::ceil(static_cast<float>(image_height) / nthread);
    for (int i = 0; i < nthread; ++i)
        threads.emplace_back(thread_main, i * numRows, 
                             std::min(numRows, image_height - i * numRows),
                             image_width);

    for (auto& x : threads)
        x.join();

    // ...
}


You may want to find some good books on multithreading, though. Keep in mind that the above probably has a few problems, and I haven't tested it.
Last edited on
Topic archived. No new replies allowed.