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

Author Topic: Print text on texture in multi-thread mode  (Read 2342 times)

0 Members and 2 Guests are viewing this topic.

alex_hmn

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Print text on texture in multi-thread mode
« on: November 06, 2017, 01:45:14 pm »
Greetings!

I have a program that uses sf::Font, sf::Texture, sf::Text and sf::RenderTexture to create multiple images with text. I'm trying to launch each image creation in a separate thread, but found out, that CPU usage is like I'm doing it single-thread and even more: result images have incorrect shifts of letters.

So the questions:
1) Is this functionality supposed to work in multi-thread mode?
2) If yes, is it possible to create images with text in parallel?

Thank you very much for you attention!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Print text on texture in multi-thread mode
« Reply #1 on: November 06, 2017, 01:48:22 pm »
The question is not "if" (of course you can do that), but "how". So show us your code and we'll tell you what's wrong.
Laurent Gomila - SFML developer

alex_hmn

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Print text on texture in multi-thread mode
« Reply #2 on: November 06, 2017, 02:31:04 pm »
Thanks for the fast reply!
I'm glad to know that it is possible.
Will prepare an example of source code.

alex_hmn

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Print text on texture in multi-thread mode
« Reply #3 on: November 09, 2017, 02:28:44 pm »
Hello again.
Here is my program.
I'm running
--font /tmp/Arial.ttf --backgrounds /tmp/bck_lst.lst --output /tmp/demo

where bck_lst.lst is a file with paths to images.
It works fine in a single thread mode, but in multi thread mode it hangs spontaneously and doesn't load CPU anyway.
Can you please have a look?

I'm glad to assist you in any way I can.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Print text on texture in multi-thread mode
« Reply #4 on: November 09, 2017, 02:45:25 pm »
I'm afraid you can't use a sf::Font concurrently, it is not thread-safe. Since it is used internally by sf::Text, you cannot add external synchronization either.

The most simple solution, although more inefficient, is to make each thread load its own local sf::Font (don't simply copy a single loaded sf::Font instance, internally it would share the same data anyway).
Laurent Gomila - SFML developer

alex_hmn

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Print text on texture in multi-thread mode
« Reply #5 on: November 09, 2017, 04:54:56 pm »
Thanks! Now it works, but still, CPU load is like in single thread mode and program creates the same amounts of images per second in both modes.
Even if I remove image saving (to exclude thoughts that it is not possible to write them this way) still I can't run it multi-thread.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Print text on texture in multi-thread mode
« Reply #6 on: November 09, 2017, 07:08:14 pm »
Just because you've thrown some threads there doesn't mean that your program will automagically run faster. You should first run some performances tests, profile your code, and find out what's the bottleneck. Chances are that it's not the CPU (are you sure that your stuff runs in parallel on the GPU too?) and that threads are totally useless. Multi-threading is not the magical solution to everything.
Laurent Gomila - SFML developer

alex_hmn

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Print text on texture in multi-thread mode
« Reply #7 on: November 09, 2017, 10:28:24 pm »
I didn't say anything about GPU and fail to see how it relates to the subject.
Also I'm not trying to solve magically everything with multi-thread and I know that when I'm using similar code without sfml-stuff it works perfectly.

I'm here to find out how to use sfml correctly and it should be possible. Can't believe that library like sfml works only single-threaded.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Print text on texture in multi-thread mode
« Reply #8 on: November 10, 2017, 06:37:01 am »
Quote
I didn't say anything about GPU and fail to see how it relates to the subject.
Well, it's simple: after loading, the CPU basically does nothing more than sending a bunch of commands to the GPU, and all the job (which consists mainly of drawing things) is done there.

Quote
Also I'm not trying to solve magically everything with multi-thread
So you've performed tests or analysis that demonstrates that more threads should theoretically speed up the process?

Quote
I know that when I'm using similar code without sfml-stuff it works perfectly.
Does "without sfml-stuff" mean "without involving the GPU"? Of course there are situations that benefit from multi-threading. But every situation is different.

Quote
I'm here to find out how to use sfml correctly
You use SFML correctly (as far as I can tell -- I didn't spend too much time on your code yet). What I'm trying to say is that the problem may be totally different from what you think. You may be using multi-threaiding incorrectly, or even everything could be correct but multi-threading has no benefit in this particular case.

Quote
Can't believe that library like sfml works only single-threaded.
It doesn't.

The best I can say is: analyze or profile your code to figure out what's really happening, ie. why your threads don't speed up the process (ie. they must be waiting on something instead of doing stuff in parallel -- hint: GPU).
Laurent Gomila - SFML developer

alex_hmn

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Print text on texture in multi-thread mode
« Reply #9 on: November 10, 2017, 10:28:03 am »
Thanks for the detailed reply and for the hint.
I'll investigate the subject.
Didn't think that we need GPU (especially when CPU idling) to print two letters simultaneously).

Now I'm going to check everything (including stuff you've mentioned) more carefully.

Thanks once again for your time and attention! =)
« Last Edit: November 10, 2017, 10:32:15 am by alex_hmn »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Print text on texture in multi-thread mode
« Reply #10 on: November 10, 2017, 10:52:27 am »
Quote
Didn't think that we need GPU (especially when CPU idling) to print two letters simultaneously
Your program basically consists of drawing stuff, and for that it is using using sfml-graphics ; sfml-graphics  internally uses OpenGL to draw stuff, and OpenGL of course uses the graphics card (through drivers) to perform this task.

So yes, your program is using the GPU, and in my opinion this is the most relevant point to consider when analyzing performances and evaluating multi-threading.
Laurent Gomila - SFML developer