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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - kaesemeister

Pages: [1]
1
System / threads and windows
« on: May 30, 2012, 09:57:30 pm »
Hi,

I recently started to work with SFML.. seems to be a really nice and slim framework which fit's my needs..
I ran into problems when using one window in multiple threads. Background: I want to split my application into different threads:

- thread which render things on screen
- thread which processes user input
- and a few more...

but sadly I can't get it to work without the following exception:
Code: [Select]
[xcb] Unknown sequence number while processing queue
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
...: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.

I called XInitThreads just to be shure that this is not the root of my problem.

My code has the following structure (simplified)

class RenderThread {
  virtual void Run() {
        while (active) {
                window.setActive(true);
                glClear(GL_COLOR_BUFFER_BIT);
                window.Display();
        }
  }
}

int main() {
        sf::Window window(sf::VideoMode(1024, 768, 32), "...");
        window.SetActive(false);
        window.UseVerticalSync(true);
       
        RenderThread renderThread(window);
        renderThread.Launch();
       
       
        sf::Event event;
        while (active) {
                // wait a bit to limit frequecy to 100Hz       
                while (window.GetEvent(event)) {
                        if (event.Type == sf::Event::Closed) {
                                active = false;
                        }
                }
        }
       
        renderThread.Wait();
        window.close();
       
        return EXIT_SUCCESS;
}
 

If i add Mutex Locks i can prevent the crash but everything get's extremly slow.

My Questions
  • Is the general concept okay?
  • When to call setActive? (it's used to connect the opengl context to the active thread, right?)
  • If the concept is bullshit, what's the right way to split eventhandling, inputhandling and rendering?
  • Is there an example which demonstrates how to do it?

Full source (it's still very simple): http://pastebin.com/hJKp7iBa


Pages: [1]
anything