Results 1 to 18 of 18

Thread: Thread problem with windows

  1. #1
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Thread problem with windows

    Hi all, I have a "strange" problem with my program when running it under linux...The programs works this way: I have GUI that, after pressing a button, starts a thread. this programs works fine under linux but crashes under windows... When I press the button. The "strange" adjective comes from the fact that this program used to work under windows also but now (after I reinstalled linux,vmware and windows) it doesn't. AFAIK the only big change i've done is the upgrade to Qt 4.1.3 from 4.1.1 (or 4.1.2 I don't remember)...
    Any hint?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Thread problem with windows

    Debug your app and see where the crash occurs.

  3. #3
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread problem with windows

    Well,it crashes when i call start()... I don't know more precisely, I have no debugger under windows, right now I'm trying to install the eclipse framework that seems nice. Oh,I'm unsing minGW. Sorry if it seems an "I_don't_want_to_bother_myself_so_I_ask_you" question but I am a bit puzzled about the behavior of a program that runned well...

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Thread problem with windows

    Quote Originally Posted by vratojr
    Well,it crashes when i call start()... I don't know more precisely, I have no debugger under windows, right now I'm trying to install the eclipse framework that seems nice. Oh,I'm unsing minGW. Sorry if it seems an "I_don't_want_to_bother_myself_so_I_ask_you" question but I am a bit puzzled about the behavior of a program that runned well...
    Use gdb to debug your app.

  5. #5
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread problem with windows

    Quote Originally Posted by wysota
    Use gdb to debug your app.
    gdb is powerfull but kinda hard to use. Dowload DrMingw, set it as default window$ debugger and you'll get the call stack when it crash. That could help, huh?
    Last edited by fullmetalcoder; 13th June 2006 at 14:31. Reason: added link to Dr Mingw
    Current Qt projects : QCodeEdit, RotiDeCode

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Thread problem with windows

    Quote Originally Posted by fullmetalcoder
    gdb is powerfull but kinda hard to use. Dowload DrMingw, set it as default window$ debuggerand you'll get the call stack when it crash. That could help, huh?
    What's so hard in a sequence of:
    $ gdb appname
    run
    bt
    quit

  7. #7
    Join Date
    Mar 2006
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread problem with windows

    The problem with gdb is that sometimes it could be interesting to see the functions called BEFORE the crash and not only the stack. In my own problem (another post), I know where it crashes (and, as it is an assert, it was not necessary) but I don't know why it crashes.
    I remember of another program, the same as gdb but with graphics, it sounded like ddd ...

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Thread problem with windows

    You can do the same with gdb. But in 90% of cases it's enough to have a backtrace to see what went wrong (a vast number of segfaults is caused by dereferencing a null pointer or corrupting the stack frame). In this case the most important is to see where in Qt library the app crashes and what was the call chain that led to the crash.

  9. #9
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread problem with windows

    Ok,I have compiled the debug libraries to finally debug the program and now the program works!!! Hrmmm.......bah......

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Thread problem with windows

    Quote Originally Posted by vratojr
    Ok,I have compiled the debug libraries to finally debug the program and now the program works!!! Hrmmm.......bah......
    Try again with release mode. Just make sure your project is cleaned and recompiled from scratch.

  11. #11
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread problem with windows

    Quote Originally Posted by wysota
    What's so hard in a sequence of:
    $ gdb appname
    run
    bt
    quit
    Dunno what's hard but I know what's driving me mad. When I use such a command I only get a call stack, and none of the graphical interface to gdb allow me to see the debug symbols from the shared librarieslinked to my program...
    Current Qt projects : QCodeEdit, RotiDeCode

  12. #12
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread problem with windows

    Well, in fact the in release mode the programs doens't work, it simply stalls as before. The problem is that I don't know how to debug it since it doens't crashes but stalls. Moreover,as I said the debug version runs correctly... How could it be?

  13. #13
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread problem with windows

    Do it the old way : uses QMessageBox::warning(), filled with crucial paramteres at points you consider as potentially problematic... I've always worked that way since I switched to Linux where, even in release mode, qDebug() works fine...
    Current Qt projects : QCodeEdit, RotiDeCode

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Thread problem with windows

    Quote Originally Posted by vratojr
    Well, in fact the in release mode the programs doens't work, it simply stalls as before. The problem is that I don't know how to debug it since it doens't crashes but stalls. Moreover,as I said the debug version runs correctly... How could it be?
    It could be that you rely on some condition which behaves differently between debug and release (for example debug might be setting all int variables to '0' and release might leave it uninitialised -- of course I don't say that it actually happens).

  15. #15
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread problem with windows

    Well I found the problem... But still I don't know why it happens... From the father of UserThread I call:
    Qt Code:
    1. if(!userThread->isRunning()){
    2. userThread->start();
    3. while(!userThread->inizializzato()); //Stalls here
    4. }
    To copy to clipboard, switch view to plain text mode 
    with
    Qt Code:
    1. void UserThread::run(){
    2. //.......
    3. _inizializzato=TRUE;
    4.  
    5. exec();
    6. }
    7.  
    8. bool UserThread::inizializzato() const {return _inizializzato;}
    To copy to clipboard, switch view to plain text mode 
    the program stalls in the "while".Now, if i run in debug mode, _inizializzato is correcly set up to TRUE and the program doesn't stall, in release mode, _inizializzato is never set to TRUE (_inzializzato is set to FALSE in the constructor) and the programs stalls.
    I suppose I can delete the problem if I wait for a signal coming from the thread instead of using that while but... Is this mine a bad style of programming?
    Thanks for the attention and the patience!

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Thread problem with windows

    Quote Originally Posted by vratojr
    Is this mine a bad style of programming?
    Yes. You should use a QWaitCondition here.

  17. #17
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread problem with windows

    Thanks, but if I am not too annoying, could you explain me why I can't use the while?Is this because I continuosly pool on a member of another thread and this generates a conflict with the owner thread? I mean, theoretically I could resolve this also by placing a mutexlocker in run() and in inzializzato() ?
    Excuse me but I want also to learn!

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Thread problem with windows

    Quote Originally Posted by vratojr
    Thanks, but if I am not too annoying, could you explain me why I can't use the while?Is this because I continuosly pool on a member of another thread and this generates a conflict with the owner thread?
    It's because you are waiting in a so called "busy loop".
    Try to compile such program:
    Qt Code:
    1. int main(){ while(1); return 0; }
    To copy to clipboard, switch view to plain text mode 
    Run it and see the CPU usage. You'll notice that it'll go up to 100%.
    Now compile and run this one:
    Qt Code:
    1. int main(){ while(1) sleep(0); return 0; }
    To copy to clipboard, switch view to plain text mode 
    This one won't eat up all your CPU power (and it'll be easier to kill such a task) -- this is called "semi-busy loop". The only difference between those two applications is that the second one forces a context switch of the processor (changes the "active" process) allowing other applications to run. As a result all your applications will run more efficiently.
    The "next step" is to use some kind of lock which will suspend your process (thread) and wake it up only where a certain condition is met. A pseudocode follows:
    Qt Code:
    1. //...
    2. // instead of while(!someEventHappened); -- busy loop
    3. // or instead of while(!someEventHappend) sleep(0); -- semi busy loop
    4. goToSleepUntil(someEventHappend);
    5. doSomething();
    6. //...
    To copy to clipboard, switch view to plain text mode 
    I mean, theoretically I could resolve this also by placing a mutexlocker in run() and in inzializzato()?
    I think so (depends where exactly you want to put it), but using a wait condition is more "elegant".

    Excuse me but I want also to learn!
    Good for you

  19. The following 2 users say thank you to wysota for this useful post:

    sunil.thaha (12th October 2006), vratojr (16th June 2006)

Similar Threads

  1. problem of porting from windows to linux
    By jyoti kumar in forum Qt Programming
    Replies: 4
    Last Post: 22nd May 2006, 09:42
  2. Problem in creating thread in GUI application
    By jyoti kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 13:05
  3. Replies: 10
    Last Post: 28th April 2006, 16:48
  4. Problem building Qt4.1.0 with thread support on windows XP
    By pavithra in forum Installation and Deployment
    Replies: 1
    Last Post: 1st April 2006, 12:35
  5. QProcess problem with windows batch file
    By bood in forum Qt Programming
    Replies: 11
    Last Post: 6th January 2006, 09:08

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.