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);
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...