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

Author Topic: Multiple threads with sf::Context and sf::Image  (Read 6796 times)

0 Members and 1 Guest are viewing this topic.

iwn

  • Newbie
  • *
  • Posts: 17
    • View Profile
Multiple threads with sf::Context and sf::Image
« Reply #15 on: May 30, 2011, 11:04:23 pm »
From a practical perspective, I'm still not sure it matters much, but from a correctness perspective I can't disagree with you. It would probably be easier to just build it correctly up front than to try and prove otherwise, so I'll give it a shot. Would you mind if I try this out and send you a patch for comment?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Multiple threads with sf::Context and sf::Image
« Reply #16 on: May 30, 2011, 11:14:14 pm »
Quote
From a practical perspective, I'm still not sure it matters much

Consider the (frequent) use case where one wants to create a window in the main thread, but draw into it in a separate thread. Before activating the window in the thread it must first be deactivated from the main thread -- otherwise SetActive(true) will throw an error.
Laurent Gomila - SFML developer

iwn

  • Newbie
  • *
  • Posts: 17
    • View Profile
Multiple threads with sf::Context and sf::Image
« Reply #17 on: May 30, 2011, 11:22:49 pm »
Yes, you are definitely correct. I hadn't considered exchanging contexts across threads like that.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Multiple threads with sf::Context and sf::Image
« Reply #18 on: May 30, 2011, 11:27:35 pm »
As far as I understand, the problem arises when you create a huge amount of graphics threads during the application lifetime.

So, is this a *real* problem? Will people need such code in their applications? What was your initial use case?
Laurent Gomila - SFML developer

iwn

  • Newbie
  • *
  • Posts: 17
    • View Profile
Multiple threads with sf::Context and sf::Image
« Reply #19 on: May 31, 2011, 12:49:36 am »
You're right, this only really shows up when you end up spawning many threads across the lifetime of the program. My initial use case involved a lazy image loader that spawned a separate thread for each image I wanted to use. Something like this:

Code: [Select]

class MyImageLoader : public Thread {
  MyImageLoader(string imageName);
  void Run() {
    image = new Image();
    // load the image data and do some moderate processing on it
    ready = true;
  }
};

class MyImage {
  MyImage(string imageName) {
    myImage = NULL;
    myLoader = new MyImageLoader(imageName);
    myLoader->Launch();
  }
  void Draw(RenderWindow window) {
    // If loader is done, take our image and cleanup the loader.
    if (myLoader && myLoader.ready) {
      myImage = myLoader->image;
      delete myLoader;
      myLoader = NULL;
    }
    // If we have our image, draw it. Otherwise, it must be
    // loading -- draw nothing.
    if (myImage) {
      window.Draw(new Sprite(*myImage));
    }
  }
};


I intended to eventually go back and rework it into some sort of single-threaded worker queue, but this did the job for the time being and it seemed to work fine until I started hitting the GL errors. SFML2 appears to have cleared those up, but by the time I had boiled it down to a minimal example, I realized it was susceptible to this memory consumption issue as well.

Even though it may be poorly architected, I don't think there's anything really wrong with the code above, and if someone wanted to write such code, I don't see why SFML should prevent or discourage them from doing so.

iwn

  • Newbie
  • *
  • Posts: 17
    • View Profile
Multiple threads with sf::Context and sf::Image
« Reply #20 on: May 31, 2011, 12:56:23 am »
Assuming, of course, that there is a reasonable and fairly straightforward solution to this ;)

I'll poke at it a bit more here and let you know if I come up with anything.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Multiple threads with sf::Context and sf::Image
« Reply #21 on: May 31, 2011, 07:38:46 am »
Ok.

And thank you very much for your help, I appreciate ;)
Laurent Gomila - SFML developer

iwn

  • Newbie
  • *
  • Posts: 17
    • View Profile
Multiple threads with sf::Context and sf::Image
« Reply #22 on: May 31, 2011, 08:17:11 am »
I've sent you a patch. Please let me know if you have trouble receiving it.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Multiple threads with sf::Context and sf::Image
« Reply #23 on: May 31, 2011, 08:49:23 am »
I got it, thanks.

I'll try to have a look at it this evening.
Laurent Gomila - SFML developer