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

Author Topic: Using multiple windows in threads  (Read 2517 times)

0 Members and 2 Guests are viewing this topic.

Jeffs

  • Newbie
  • *
  • Posts: 4
    • View Profile
Using multiple windows in threads
« on: March 27, 2011, 01:19:36 am »
Hi! I've created a plot class, where one can make an object that plots some data. I want this plot to appear in its own window, and be totally free of the main application. This way, one can update the plot without having to create another window. So I made the plot class inherit from sf::thread, and in the run() function I put a regular window loop. I have the following problems:

1. If I close the plot window before the window in the main application, I get an access violation error. This happens in the destructor of sf::thread. (When wait() runs WaitForSingleObject).

2. If I close the main window before the plot window, and then let the main thread sleep for a while, the plot window will just freeze.

3. If I don't open any windows in the main thread, but just let it sleep, the plot window just becomes black.

I'm wondering if I'm going about this problem the right way, and if so, what can be the cause of my problems? Everything works just fine without the threading stuff, so I'm pretty sure it has something to with that.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Using multiple windows in threads
« Reply #1 on: March 27, 2011, 11:19:30 am »
Show us your code -- at least a simplified version so that we can understand how you handle your windows in threads.
Laurent Gomila - SFML developer

Jeffs

  • Newbie
  • *
  • Posts: 4
    • View Profile
Using multiple windows in threads
« Reply #2 on: March 27, 2011, 01:29:54 pm »
Thanks for the quick reply!

Simplifying the code helped! I just noticed that I created my window in the constructor of the plot class, and not in the run() function. Therefore, the window was actually created in the wrong thread. It seems this causes an access violation upon terminating the thread. I'm not sure why though, since data should be shared between threads..
[/code]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Using multiple windows in threads
« Reply #3 on: March 27, 2011, 01:37:54 pm »
This is an OS limitation, windows must be created in the same thread as you process their events.
Laurent Gomila - SFML developer

Jeffs

  • Newbie
  • *
  • Posts: 4
    • View Profile
Using multiple windows in threads
« Reply #4 on: March 27, 2011, 01:49:21 pm »
Ah, I would never have known. Thanks for the help.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Using multiple windows in threads
« Reply #5 on: March 27, 2011, 02:52:41 pm »
Moreover, if you're planning to deploy your application on OS X you have to create and process event of all your windows in the main thread. This is very annoying but there is no workaround.
SFML / OS X developer

 

anything