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

Author Topic: Window and multiple threads  (Read 3310 times)

0 Members and 1 Guest are viewing this topic.

tymofey

  • Newbie
  • *
  • Posts: 7
    • View Profile
Window and multiple threads
« on: September 19, 2011, 07:51:15 pm »
I have a thread one where i create the window:
Code: [Select]
sf::Window* wnd = new sf::Window(sf::VideoMode(400, 300), "some title", sf::Style::Close);
Then i pass the wnd ponter to two other threads.
In thread two i query window for events:
Code: [Select]
sf::Event event;
while (true)
    while(wnd->PollEvent(event))
    {
         //....
    }

And in thread three I draw in it:
Code: [Select]
while (true)
{
    //...
    wnd->Display();
}


The window is of course protected by proper locking.

This causes lots of
Quote
Failed to activate window's context
messages to the console. Is what i am trying to do even possible?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Window and multiple threads
« Reply #1 on: September 19, 2011, 08:35:47 pm »
You must first deactivate (SetActive(false)) the window in the thread where it is active (->the thread where you created it), before activating it in another thread.
Laurent Gomila - SFML developer

tymofey

  • Newbie
  • *
  • Posts: 7
    • View Profile
Window and multiple threads
« Reply #2 on: September 19, 2011, 08:42:39 pm »
Would doing that really often (like every frame) introduce huge overhead?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Window and multiple threads
« Reply #3 on: September 19, 2011, 09:25:23 pm »
Yes. But why would you need to draw to the same window from multiple threads?
Laurent Gomila - SFML developer

tymofey

  • Newbie
  • *
  • Posts: 7
    • View Profile
Window and multiple threads
« Reply #4 on: September 19, 2011, 10:15:50 pm »
i dont. i want to draw in one thread and poll events in another. but it appears that i still need the window to be active to poll events from it.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Window and multiple threads
« Reply #5 on: September 19, 2011, 10:19:58 pm »
Nop. SetActive activates the OpenGL context, which is necessary only for drawing operations.
Laurent Gomila - SFML developer

tymofey

  • Newbie
  • *
  • Posts: 7
    • View Profile
Window and multiple threads
« Reply #6 on: September 19, 2011, 10:47:10 pm »
then appearently i am doing something really wrong as the window does not seem to generate any events this way

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Window and multiple threads
« Reply #7 on: September 19, 2011, 11:02:32 pm »
Some OSes (like Windows) require events to be polled from the thread that created the window.
Laurent Gomila - SFML developer

tymofey

  • Newbie
  • *
  • Posts: 7
    • View Profile
Window and multiple threads
« Reply #8 on: September 19, 2011, 11:07:17 pm »
thanks, that explains everything. i believe this should be mentioned in the docs.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Window and multiple threads
« Reply #9 on: September 20, 2011, 07:49:32 am »
Quote
i believe this should be mentioned in the docs

I agree ;)
Laurent Gomila - SFML developer