Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Noise when using threads  (Read 1717 times)

0 Members and 1 Guest are viewing this topic.

AjgorIgnacy

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Noise when using threads
« on: February 01, 2020, 11:21:10 am »
Hello
I am creating a fractal exploration program. Such tasks require a lot of computational power. During my first tests I managed to reach ~15% of processor load, very poor FPS and couldn't force it to work faster. Some people reccomended trying multithreading my program. So I did it. But then a weird thing happens. Depending on the PC the program is running at, I sometimes get terrible image noise as shown in noise.png. But on other PCs everything is ok and processor reaches 90% usage.
Entire code is in main.cpp and free to use/copy.

So here are my queries:
1. Is this caused by the way I manage threads?
2. Is there a better way to display fractals than by using sf::Image?
3. Any other performance/general tips?

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Noise when using threads
« Reply #1 on: February 01, 2020, 01:01:40 pm »
since all threads using same resource you need to lock the current thread by using "std::mutex" or even better "std::lock_guard<std::mutex>".

to fix the processor usage consumed, you have to make the current active thread to sleep for sometime. there is a function already made for this purpose: "std::this_thread::sleep_for(std::chrono::microseconds(1))"

in general sf::Image was not made to do a heavy image processing, alternatively you you may look for computing shader and indirect draw commands, it is available in both opengl and D3D.
it will help you if you need to boost your application further.

this is what i know as common advises maybe there is better way to handle the image processing efficiently that i'm not aware of.
« Last Edit: February 01, 2020, 01:06:43 pm by Mortal »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Noise when using threads
« Reply #2 on: February 01, 2020, 03:02:44 pm »
You should offload computation to multiple threads, not rendering. Rendering should happen in the main thread, and it's likely not the CPU bottleneck.

You need to synchronize access to shared resources from your threads (C++11 has a lot of threading primitives).

I wouldn't use sf::Image, it's a RAM-based pixel container and not made for fast graphics updates. There are multiple options what to do instead, e.g. you could use a sf::RenderTexture (don't forget to call display()), and render points to it with sf::VertexArray.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Noise when using threads
« Reply #3 on: February 02, 2020, 04:30:26 pm »
hmm, interesting i haven't thought about drawing the pixels as points with sf::VertexArray, i think this worth testing. gotta make my own mandelbrot demo  ;D

 

anything