Results 1 to 4 of 4

Thread: Blocking a worker thread while waiting for user input

  1. #1
    Join Date
    Jun 2015
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Blocking a worker thread while waiting for user input

    I have a worker object on a thread that is processing files. In some cases, the file needs user input to continue. So I need my worker to block until the user gives the input, and then it can continue.

    Qt Code:
    1. class Worker : public QObject
    2. {
    3. Q_OBJECT(Worker)
    4. public:
    5. Worker();
    6. ~Worker();
    7.  
    8. protected slots:
    9. void process()
    10. {
    11. ...
    12. if ( needInputFromUser )
    13. emit signal_openDialog();
    14.  
    15. // Continue with processing
    16.  
    17. ...
    18. }
    19.  
    20. signals:
    21. signal signal_openDialog();
    22. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class OnTheGUIThread : public QWindow
    2. Q_OBJECT(OnTheGUIThread)
    3. public:
    4. OnTheGUIThread();
    5. ~OnTheGUIThread();
    6.  
    7. public:
    8. void whereWorkerIsStarted()
    9. {
    10. ...
    11. QThread *thread = new QThread;
    12. Worker *worker = new Worker;
    13.  
    14. connect(thread,SIGNAL(started()),worker,SLOT(process()));
    15. connect(worker,SIGNAL(signal_quit()),thread,SLOT(quit()));
    16.  
    17. connect(worker,SIGNAL(signal_openDialog()),this,SLOT(slot_openDialog()),Qt::BlockingQueuedConnection);
    18.  
    19. thread->start();
    20.  
    21. ...
    22.  
    23. }
    24.  
    25. protected slot:
    26. void slot_openDialog()
    27. {
    28. dialog.exec(); // Blocks until window closed;
    29. worker.setData(dialog.data());
    30.  
    31. return;
    32. }
    To copy to clipboard, switch view to plain text mode 

    If I connect the openDialog signal/slots with a Qt::BlockingQueuedConnection, I get a deadlock. According to the documentation:

    Same as QueuedConnection, except the current thread blocks until the slot returns. This connection type should only be used where the emitter and receiver are in different threads.

    They are in different threads (or are they?), and I want the current thread to block until the slot returns. Am I just misunderstanding what it's used for?

    Secondary question, what's the best way to pass data back to the processing thread?

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Blocking a worker thread while waiting for user input

    Hi, I don't see where you move the worker to the QThread you created. Did you just omit this from your post or have you not done that?

  3. #3
    Join Date
    Jun 2015
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Blocking a worker thread while waiting for user input

    SOLVED!

    I DIDN'T move the object to the thread. I was getting the desired effect (GUI updating while object ran in the background), but it wasn't until i tried this that it became a problem.

    That's why I was so confused why the BlockingQueuedConnection wasn't working like I thought it should.

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Blocking a worker thread while waiting for user input

    Heh, glad I could help.

    Regarding your other question about how to move data between threads, I've always used signal/slots since it simplifies passing data rather than having to lock a data structure with a mutex, etc. To each his own I guess, but using signals/slots hasn't presented any specific challenges and it just works.

Similar Threads

  1. Replies: 4
    Last Post: 17th October 2013, 11:12
  2. Replies: 5
    Last Post: 22nd June 2012, 16:40
  3. Replies: 8
    Last Post: 26th March 2010, 12:45
  4. Waiting on a thread?
    By MrGarbage in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2007, 16:13
  5. Main thread - worker thread communication.
    By kikapu in forum Newbie
    Replies: 25
    Last Post: 23rd May 2007, 22:09

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.