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:
while(!stopCondition) { processEvents(); }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.






Reply With Quote
Bookmarks