implemented working multithreading

This commit is contained in:
2022-07-05 23:22:01 +02:00
parent 22892c3d70
commit c79412bdab
3 changed files with 93 additions and 16 deletions

View File

@ -9,26 +9,52 @@ Mandelbrotc::Mandelbrotc(Vec2mp const &f, Vec2mp const &t, Vec2i const &s, uint3
: f(f), t(t), s(s), max_iter(mi)
{
this->threads = std::vector<std::thread>();
this->screen = std::vector<std::vector<uint8_t>>();
//this->screen = std::vector<std::vector<uint8_t>>();
this->screen = new uint8_t*[s.x];
for(int i = 0; i < s.x; ++i)
this->screen[i] = new uint8_t[s.y];
this->done = false;
}
void Mandelbrotc::start_threads()
void Mandelbrotc::start_threads(bool first)
{
if (!this->done && !first)
return;
Mandelbrotc const &m = *this;
for (int i = 0; i < this->thread_count; i++) {
std::thread t = std::thread(&Mandelbrotc::calc, this, i);
this->threads.push_back(std::move(t));
std::cout << "thread created" << std::endl;
}
this->done = false;
}
void Mandelbrotc::stop_threads()
{
for (int i = 0; i < this->thread_count; i++) {
while (!this->threads[i].joinable())
this->threads[i].join();
}
}
void Mandelbrotc::calc(const uint8_t tid)
{
mpreal x, y;
std::vector<uint8_t> v = std::vector<uint8_t>();
for(double i = tid; i < this->s.x; i+=this->thread_count) {
for(double j = 0; j < this->s.y; j++) {
x = (mpfr::abs(this->f.x - this->t.x) * (i / this->s.x)) + this->f.x;
y = (mpfr::abs(this->f.y - this->t.y) * (j / this->s.y)) + this->f.y;
this->screen[i][j] = mandelbrot(Vec2mp(x, y));
this->screen[(int)i][(int)j] = this->mandelbrot(Vec2mp(x, y));
}
}
std::cout << "thread " << tid << " done" << std::endl;
if (tid == thread_count-1)
this->done = true;
}
uint32_t Mandelbrotc::mandelbrot(const Vec2mp &n)