Results 1 to 8 of 8

Thread: How to seperate my program into two threads

  1. #1
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default How to seperate my program into two threads

    I have a program that is separated into two projects the GuiLayer and the Datalayer both are QT based the Datalayer is compiled into a .dll that the guilayer references and objects are passed between them.

    For example the user requests a search, the guilayer creates a searchObject fills in the query parameters passes it to the datalayer, which then makes a request on the server (though later it will optionally query a local database), when result returns the datalayer sets the results on the searchobject and the searchObject emits a signal telling the gui layer results are available.

    The problem I have run into is the Datalayer does a lot of xml parsing and while it's parsing xml it blocks gui updates, the cursor in the lineedit stops blinking, you can't move the program etc, so I would like to have the datalayer run in it's own thread so that things like that can't happen but I am not exactly sure how to do that.

  2. #2
    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: How to seperate my program into two threads

    Sounds like a case for QtConcurrent
    ==========================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.

  3. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to seperate my program into two threads

    It is even simpler. If you created nice QObject for data layer with signals and slots then you just QObject::moveToThread giving as argument standard thead (no subclasing is needed) then do needed connections between data layer and user UI.
    Thats is all you need to do .


    Added after 4 minutes:


    simple example:
    Qt Code:
    1. MainWindow::MainWindow(QObject *parent) : QMainWindow(parent)
    2. {
    3. QThread* thread = new QThread(this);
    4. datalayer = new DataLayerClass();
    5. datalayer ->moveToThread(thread);
    6. datalayer->setParent(this); // parent can't be set before moveToThread
    7.  
    8. connect(this, SIGNAL(sendRequest(QString)), datalayer, SLOT(startRequest(QString)));
    9. connect(datalayer, SIGNAL(someResultReady1(QString)), this, SLOT(setLabel(QString)));
    10. connect(datalayer, SIGNAL(someResultReady2(QString)), this, SLOT(setEdit(QString)));
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by MarekR22; 3rd March 2011 at 16:23.

  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: How to seperate my program into two threads

    I personally think using QtConcurrent is simpler. Here there is a problem what to do with the thread once you're done with it.

    By the way, I think line #6 in your code is invalid. What would happen if "this" was deleted?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to seperate my program into two threads

    Quote Originally Posted by wysota View Post
    By the way, I think line #6 in your code is invalid. What would happen if "this" was deleted?
    Yep you are rigth! Thread pointer should be stored as a field and then in destructor:
    Qt Code:
    1. MainWindow::~MainWindow()
    2. {
    3. thread->exit();
    4. thread->wait(2000);
    5. thread->deleteLater();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Anyway can you give this simple example with QtConcurrent?

  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: How to seperate my program into two threads

    Quote Originally Posted by MarekR22 View Post
    Anyway can you give this simple example with QtConcurrent?
    It could be something like this:
    Qt Code:
    1. Result parseXml(const QString &xml) { ... }
    2. // ...
    3. QString xml = ...;
    4. QFuture<Result> result = QtConcurrent::run(parseXml, xml);
    5.  
    6. // optionally:
    7. QFutureWatcher<Result> m_watcher;
    8. m_watcher.setFuture(result);
    9. connect(&m_watcher, SIGNAL(finished()), ..., ...);
    10.  
    11. Result res = result.result(); // or m_watcher.future().result();
    To copy to clipboard, switch view to plain text mode 
    The main advantage over your solution is that if you have a machine with 4 cores, you can run 4 such functions concurrently and with your solution you only have one thread so requests are queued and some CPUs are left idle. Besides I think using QtConcurrent is just much simpler Especially that you don't need the optional part since calling QFuture::result() will block if the result is not ready yet. So you can even have a flow such as this:
    Qt Code:
    1. QFuture<...> future = QtConcurrent::run(something); // start calculations you'll need later
    2. // more code here to prepare final operation
    3. Result result = future.result(); // may block
    4. doSomethingWith(result, otherThings);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to seperate my program into two threads

    @wysota

    I believe lines 8 and 9 of your code should be swapped, according to the documentation.

  8. #8
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to seperate my program into two threads

    hmmm what about using a QRunnable for the parser?

    Qt Code:
    1. class MyXmlParser : public QRunnable
    2. {
    3. void run()
    4. {
    5. //parse
    6. searchObject->setResults(parsingresult); //This causes the searchObject to emit the signal that the guilayer is waiting for
    7. }
    8. };
    9.  
    10. QThreadPool::globalInstance()->start(new MyXmlParser(searchObject, toBeParsedString));
    To copy to clipboard, switch view to plain text mode 

    Are there any other big downsides to this solution? I think it is very similar to what is done with QtConcurrent::run?

    I was thinking to run the datalayer completely in it's own thread (there can only ever be one datalayer object) then change the gui/datalayer communication to be purely signal/slot based and within the datalayer itself move the parsing code into runnables. Then I can be completely sure the datalayer never blocks the gui no matter who puts code into it.

    When using runnables is it necessary to make any calls to moveToThread(); I am honestly a bit confused on when you need to use that function.

Similar Threads

  1. Starting QT GUI in a seperate Thread
    By bbui210 in forum Qt Programming
    Replies: 8
    Last Post: 27th June 2018, 16:17
  2. FTP Program using multiple Threads
    By neveffects in forum Qt Programming
    Replies: 5
    Last Post: 3rd January 2011, 05:33
  3. Replies: 1
    Last Post: 17th April 2010, 00:12
  4. Adding new Widgets in a seperate thread
    By Cruz in forum Qt Programming
    Replies: 7
    Last Post: 16th January 2009, 18:20
  5. uic not creating seperate .h and .cpp files
    By Kapil in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2006, 10:43

Tags for this Thread

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.