Results 1 to 2 of 2

Thread: Accessing A single window from different threads

  1. #1

    Default Accessing A single window from different threads

    I recently ran into some troubles while attempting to access the same (global) CL_DispayWindow-- a static CL_DisplayWindow pointer returned from a factory-esk function-- from two different threads. The following code illustrates the scenario:

    Code:
    #include <core.h>
    #include <display.h>
    #include <gl.h>
    #include <application.h>
    
    void close(void)
    {
    	std::exit(EXIT_SUCCESS);
    }
    
    CL_DisplayWindow* window_factory(void)
    {
    	static CL_DisplayWindow wnd("Window", 640, 480);
    	return &wnd;
    }
    
    class _Mythread
    {
    public:
    	void run(void)
    	{
    		CL_Slot exit_slot = window_factory()->sig_window_close().connect(&close);
    
    while(window_factory()->get_ic().get_keyboard().get_keycode(CL_KEY_ESCAPE)              == false)
    		{
    			window_factory()->flip(1);
    			CL_KeepAlive::process();
    		}
    		close();
    	}
    };
    
    CL_Thread thread;
    
    class Program
    {
    public:
    	static int main(const std::vector<CL_String>&)
    	{
    		CL_SetupCore setup_core;
    		CL_SetupGL setup_GL;
    		CL_SetupDisplay setup_display;
    
    		//window_factory()->flip(1);
    
    		thread.start(&_Mythread(), &_Mythread::run);
    		thread.join();
    
    		return 0;
    	}
    };
    
    
    CL_ClanApplication application(&Program::main);
    This actually works fine. However, when I un-comment the
    Code:
    window_factory->flip(1);
    statement within the
    Code:
    Program::main
    method, the debugger gives me an error stating that an invalid memory location was read when attempting to perform the framebuffer flip in the
    Code:
    _Mythread::run
    method.

    Can anyone provide me with insight as to why this is occurring and how to fix it?

    EDIT: Is it only possible for a thread in which the window was created to make changes to that window?
    Last edited by Lars; 04-18-2011 at 01:51 AM.

  2. #2
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    554

    Default

    As a rule of thumb that really almost applies to any graphical framework out there, including GDI, X11, Winforms, OpenGL, Direct3D, Cocoa and Cocoa Touch you really should stick to using one thread for the UI. And you should always create the graphical objects on the thread that does the UI. For clanlib this means the thread that creates CL_DisplayWindow should also do the painting and flipping.

    Why? Almost all UI frameworks are designed around the concept of having some kind of application run loop that dispatches messages such as repaint request, input events and so on. The general approach is that when an object is created (such as a window) the object is bound automatically to the run loop / message queue belonging to that thread. The rules for how to interact with UI objects safely between threads are fairly complex, poorly documented and varies from platform to platform. As a result almost no applications do stuff like this.

    When it comes to OpenGL the driver usually spawns several threads that assist it in managing the context. Calling flip() does not always suspend the thread until the flipping has completed. Instead the driver usually informs one of its worker threads that it should flip the buffers as soon as all the OpenGL commands buffered on the GPU has completed. I don't know the exact details of how Windows, GDI and the OpenGL driver interact but it is not unlikely some thread message passing goes on to finalize the flip. My guess is that the first flip() causes this message passing to be somehow bound to the first thread and that confuses the driver and the whole thing comes crashing down in the second flip().

    TL;DR version: Don't do UI/gfx calls in multiple threads. It is not worth it.

Similar Threads

  1. How to : Delete/Close a Window and Center a Window ?
    By Fr3DBr in forum Official ClanLib SDK Forums
    Replies: 0
    Last Post: 08-24-2009, 02:55 AM
  2. Multiple "keydown" signals on a single keystroke
    By zag in forum Official ClanLib SDK Forums
    Replies: 1
    Last Post: 10-19-2008, 11:04 PM
  3. signals & threads
    By wamma in forum Official ClanLib SDK Forums
    Replies: 1
    Last Post: 04-18-2008, 10:34 PM
  4. Threads
    By catch22 in forum Official ClanLib SDK Forums
    Replies: 5
    Last Post: 06-30-2007, 04:54 PM
  5. Accessing the global application instance
    By Iori Branford in forum Official ClanLib SDK Forums
    Replies: 2
    Last Post: 01-05-2007, 07:01 AM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •