Results 1 to 8 of 8

Thread: How to Have a class run in a separate thread.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to Have a class run in a separate thread.

    Quote Originally Posted by sona1111 View Post
    I am having trouble understanding how the "event loops" are supposed to run in a similar manner to classes.
    They are not since classes (and objects) do not "run" in any way. The only thing that an object does is that it "exists".

    An event loop is basically this code:

    Qt Code:
    1. while(!stopCondition) {
    2. processEvents();
    3. }
    To copy to clipboard, switch view to plain text mode 

    with "processEvents" being a call which takes an event out of a queue and processes it.

    You can compare "objects" to books and threads to people reading books. If you have two books, it is not true that there are separate people reading them just because they are different books. When one book has a reference to another book, reading that book does not require a new person -- the initial person just puts the original book aside, takes the other one, reads what needs to be read, puts that book aside and comes back to the first one. In this situation the process of reading the first book is "frozen" for the duration of reading the other book. The same applies if the book refers to itself -- you open the book on a different page, read what you need and go back to the previous context. The basic principle is that you can read only one line of text from one book at a time and that you read lines in sequence or can jump to a different line and start reading there.

    With multiple people (multiple threads) they can read two books at the same time or they even can read different pages of the same book at the same time (however with the latter, they have to be more careful not to disturb each other) or even read the same lines of the same book concurrently provided if some conditions are met (they can both see the text and they "take notes" of the content on different sheets of paper instead of say... writing on the margins of the book directly).

    If you read book A and you want some information from book B but you want to save some time and continue reading book A, then it doesn't happen automatically that your friend reads book B for you and hands over his notes related to what you need from the other book. You have to tell the guy "hey my good friend, could you read page 42 from book B for me and tell me what it is about?" thus you send a message to your friend asking him for a favour. If you do that in a wrong moment you might disturb your friend the same way as he would make you very angry if he tried to give you the information you needed while you were taking your own notes and you forgot half of what you wanted to write because of him talking to you or worse writing down his answers on your piece of paper over the text you have already written. You and your friend need to synchronize how you exchange information.

    This is exactly the same with threads. If you want a worker thread to do something for you then you need to ask for it and when the response is ready the other thread has to know how to return that info to you. Qt provides two built-in mechanisms for that -- events and signals and slots and apart from those two you can build your own mechanisms based on direct data exchange provided you do proper synchronization using synchronization primitives such as mutexes and wait conditions.

    Basically you will either have to wait until your friend finishes reading what you wanted him to read for you (which usually defeats the whole purpose of cooperating with a different "person" as you might have as well read that other book yourself then) or you need a way to be notified about the data when it is ready.
    Last edited by wysota; 29th August 2013 at 06:49.
    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.


Similar Threads

  1. OpenGL rendering in separate thread, Qt 5.0.1
    By jaf in forum Qt Programming
    Replies: 3
    Last Post: 27th October 2015, 23:03
  2. GUI Updates from separate Thread
    By sa5webber in forum Newbie
    Replies: 5
    Last Post: 16th June 2012, 20:08
  3. Put in a separate thread a method of a class
    By franco.amato in forum Qt Programming
    Replies: 0
    Last Post: 19th March 2010, 23:45
  4. new QWidget in separate thread
    By magland in forum Qt Programming
    Replies: 15
    Last Post: 7th February 2008, 12:32
  5. handling paintGL in a separate thread
    By al101 in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2007, 17:04

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
  •  
Qt is a trademark of The Qt Company.