Results 1 to 5 of 5

Thread: Multithreading problem

  1. #1
    Join Date
    Sep 2007
    Location
    Vienna, Austria
    Posts
    19
    Thanks
    10
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Multithreading problem

    Hi, my current situation is this:

    I'm using threads in my application and i want to communicate with the main thread.
    At the moment I'm doing it like this:

    thread:
    Qt Code:
    1. [...]
    2. extern QMutex waitForUserLock;
    3. extern QWaitCondition waitForUser;
    4. [...]
    5. text = [...];
    6. customEvent * send_event = new customEvent(type);
    7. send_event->setData((void*)text);
    8.  
    9. waitForUserLock.lock();
    10. QApplication::postEvent((QObject*)mainwindow, send_event);
    11. waitForUser.wait(&waitForUserLock); //sometimes hangs here
    12. waitForUserLock.unlock();
    13. [...]
    To copy to clipboard, switch view to plain text mode 

    mainwindow:
    Qt Code:
    1. [...]
    2. QMutex waitForUserLock;
    3. QWaitCondition waitForUser;
    4. [...]
    5. void mainwindow::customEvent(QEvent *e)
    6. {
    7. customEvent *me = (customEvent *) e;
    8. switch (me->type())
    9. {
    10. case type:
    11. waitForUserLock.lock();
    12. my_function((char*)me->data());
    13. waitForUser.wakeOne();
    14. waitForUserLock.unlock();
    15. return;
    16. [...]
    17. }
    18. }
    19. [...]
    To copy to clipboard, switch view to plain text mode 

    The problem is the wait, where the application sometimes hangs.
    Does anyone know where the mistake is or is there a better solution?

  2. #2
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading problem

    you can use signals and slots for cross thread communication.
    Meaning: you emit a signal in one thread and catch it in a slot in the main thread.

    Good luck,

    Tom

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading problem

    From the code you posted its hard to tell.
    It sounds like you are having a dead lock
    Two things:
    1. Don't use gloabl mutexes, and remember, you should mutex data /resources , not code.
    2. Try using tryLock().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. The following user says thank you to high_flyer for this useful post:

    y.shan (13th December 2007)

  5. #4
    Join Date
    Sep 2007
    Location
    Vienna, Austria
    Posts
    19
    Thanks
    10
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: Multithreading problem

    Quote Originally Posted by DeepDiver View Post
    you can use signals and slots for cross thread communication.
    Meaning: you emit a signal in one thread and catch it in a slot in the main thread.

    Good luck,

    Tom
    yeah, but isn't this the same as using events?

  6. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading problem

    yeah, but isn't this the same as using events?
    Yes it is, its what Qt4 does in the background with the signals.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 07:08
  2. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  3. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.