Results 1 to 5 of 5

Thread: Problems with windows and threads

  1. #1
    Join Date
    Jun 2009

    Default Problems with windows and threads


    I just started using (or learning) ClanLib and I already like it a lot. But I got two problems when I open windows from different threads.

    The first problem is, that sometimes (about 1 out of 10) when I use fonts they look strange or the program crashes under linux.

    The second problem is a segfault when the window is closed.

    I took your Hello World example and added some threads to reproduce the problem. Maybe I just overlooked some basic things...?

    I'm using ClanLib 2.0 svn.

    #include <ClanLib/core.h>
    #include <ClanLib/display.h>
    #include <ClanLib/gl.h>
    #include <ClanLib/application.h>
    class DisplayProgram
    	static int main(const std::vector<CL_String> &args);
    class MyWin
    	void win(const char* title);
    // Create global application object:
    // You MUST include this line or the application start-up will fail to
    // locate your application object.
    CL_ClanApplication app(&DisplayProgram::main);
    int DisplayProgram::main(const std::vector<CL_String> &args)
    	// Setup clanlib modules:
    	CL_SetupCore setup_core;
    	CL_SetupDisplay setup_display;
    	CL_SetupGL setup_gl;
    	MyWin w1,w2;
    	CL_Thread t1;
    	CL_Thread t2;
    	return 0;
    void MyWin::win(const char* title)
    	// Create a window:
    	CL_DisplayWindow window(title, 640, 480);
    	// Retrieve some commonly used objects:
    	CL_GraphicContext gc = window.get_gc();
    	CL_InputDevice keyboard = window.get_ic().get_keyboard();
    	CL_Font font(gc, "Tahoma", 30);
    	// Loop until user hits escape:
    	while (!keyboard.get_keycode(CL_KEY_ESCAPE))
    		// Draw some text and lines:
    		CL_Draw::line(gc, 0, 110, 640, 110, CL_Colorf::yellow);
    		font.draw_text(gc, 100, 100, title, CL_Colorf::red);
    		// Make the stuff visible:
    		// Read messages from the windowing system message queue,
    		// if any are available:
    		// Avoid using 100% CPU in the loop:

  2. #2
    ClanLib Developer
    Join Date
    May 2007


    There are 3 issues here:

    1) Opening windows from different threads.
    As far as I know, the Win32 platform does not allow you do create windows from different threads, in the same application (But I could be wrong!)

    In Sources/Display/setup_display.cpp, I was going to add this following line:
    But, this assumes the Linux target will use Xlib - Which it may not do.
    So, in the end, I added up, but commented it out.

    (See - )

    2) CL_System::sleep(10);
    Although, in your situation, this is okay. (But window.flip(1) is a better choice imo, syncing to VBL, freeing cpu time that way)

    If CL_DisplayMessageQueue:rocess(-1); is used, i.e. Only return when a message has been processed (including a CL_Timer event)

    The CPU usage is still 100%, because the CL_DisplayMessageQueue_X11::msg_wait_for_multiple_ objects() function does a while(true) loop waiting for events, instead of using select() to wait for events.

    This needs fixed, if possible.

    3) Fonts - Damm linux fonts!

    If you are using ubuntu, this is connected to the bug

    I thought I had fixed it by changing the Freetype hinting from FT_LOAD_TARGET_NORMAL to FT_LOAD_TARGET_LIGHT ,but sometimes even that does not work (and is only a temporary fix!)

    It can be fixed by recompiling the freetype2 package
    (And modifying CL_FreetypeFont::create_pixelbuffer()'s FT_Load_Glyph() parameter.

  3. #3
    Master Sorcerer
    Join Date
    Sep 2006


    Although I haven't tested this program of yours, it should work in Windows. If it doesn't, I can have a look at that.

    In Windows each thread has its own message queue and the windows and other graphics resources created belong to the thread creating them. Just like your app assumes it should.

    However, it seems the Linux version of ClanLib does currently not follow this rule. To make it work the same way, the Linux version needs to call XOpenDisplay for each thread and thus use a different Display object for each thread. I haven't looked at the Linux implementation for ClanLib, so I don't know if this is a trivial change or not - but I suppose someone would have to volunteer to make it.

  4. #4
    Join Date
    Jun 2009


    Hi, thanks for your fast replies.

    The program crashes under windows and linux when one of the threads ends at least when I use the OpenGL-Target. With the GDI-Target it works as I expected. The SDL-Target under linux crashed a bit earlier when the windows are created.

    The XInitThreads(); line is still in the svn (not commented out) but I tried both with and without it and it didn't seem to make a difference

    Thanks for the hints on freeing cpu cylcles. I just took the loop from the Display "Hello World" Example in the documentation but in my program I will try to optimize on this.

    On the font issue. It is still a very strange. In the example I posted it isn't a big issue also I'm able to start the Font example many times without a problem but in the program I'm working on there is very often the exception "freetype: error loading glyph". Sometimes it crashes and sometimes the font is not correct i.e. a big "y" is printed instead of "f" or things like that. And then it also very often works without problems. Im under Gentoo and it's using FreeType version 2.3.9 and a patch according to
    The crashes give different errors. One was "Xlib: charsets ISO8859-1:GR and ISO8859-1:GR have the same CT sequence". Any idea?

  5. #5
    ClanLib Developer
    Join Date
    May 2007


    Quote Originally Posted by rombust View Post

    The CPU usage is still 100%, because the CL_DisplayMessageQueue_X11::msg_wait_for_multiple_ objects() function does a while(true) loop waiting for events, instead of using select() to wait for events.
    That has been fixed in SVN.

    Note, in the basic2d example:
    CL_DisplayMessageQueue::process(0) ... Returns straight away
    CL_DisplayMessageQueue::process(-1) ... Returns on a message (eg mouse movement)
    CL_DisplayMessageQueue::process(1000) ... Should return on a message or if 1 second has elapsed.

    But due to a seperate bug (or myself misunderstanding process() ), it always lasts for 1 second (Win32 and Linux I believe)
    Last edited by rombust; 06-12-2009 at 12:46 PM. Reason: Comment about CL_DisplayMessageQueue::process()

Similar Threads

  1. Server, Signal and Threads problems
    By Sld in forum Official ClanLib SDK Forums
    Replies: 0
    Last Post: 05-03-2008, 03:43 PM
  2. Windows Bug
    By Starclopsofish in forum Novashell Game Creation System
    Replies: 2
    Last Post: 04-25-2008, 05:00 AM
  3. signals & threads
    By wamma in forum Official ClanLib SDK Forums
    Replies: 1
    Last Post: 04-18-2008, 10:34 PM
  4. Setup networking in multiple threads?
    By catch22 in forum Official ClanLib SDK Forums
    Replies: 0
    Last Post: 07-17-2007, 11:55 PM
  5. Threads
    By catch22 in forum Official ClanLib SDK Forums
    Replies: 5
    Last Post: 06-30-2007, 04:54 PM



Posting Permissions

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