Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threads & Event loop in the Qt application [closed]

Can anyone explain difference in the Threads and Event loop, and how can I use this in the QT application.

where can I use move to the thread and complete thread class.?

like image 732
tharunkumar Avatar asked Mar 24 '16 13:03

tharunkumar


People also ask

Is Threads the scariest movie ever?

The critical consensus reads: "An urgent warning against nuclear conflict, Threads is a chilling hypothetical that achieves visceral horror with its matter-of-fact presentation of an apocalypse." Threads is regarded as one of the most terrifying films ever made.

What is thread with example?

Thread is often referred to as a lightweight process. The process can be split down into so many threads. For example, in a browser, many tabs can be viewed as threads. MS Word uses many threads - formatting text from one thread, processing input from another thread, etc.

How realistic is the movie Threads?

"Threads" is a realistic film still impressive in 2021. In 1984 it was scarier with the Cold War, but in the present days it is still frightening since unstable Powers that Be may press the feared button. The effects of the nuclear holocaust in the population of Sheffield is dreadful.

What happens to the baby in Threads?

She is told to go elsewhere, but after repeating herself several times she is assigned to a bed. She gives birth to a horribly deformed, stillborn infant. As it is passed to her, she looks at it, and opens her mouth to scream, but the audience hears nothing but the howling wind.


2 Answers

Each thread processes its own event loop, you normally do not need to worry about this - its taken care of for you and unless you have a specific reason to its meant to be left alone.

QThread is a class provided by Qt for you to use to control the operation of a thread. The method of "putting" object into that thread is to use the moveToThread() function.

You should not inherit the QThread class in order to run some code inside a thread (use the moveToThread function), the only reason to inherit the QThread class is if you want to change the behaviour of the thread control.

Below are the basic steps to get an object running inside a thread:

MyObj *myObj = new MyObj(0); // 0 = no parent if your object inherits QObject
QThread* thread = new QThread;
myObj->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), myObj, SLOT(run()));
thread->start();

Once you call start() the thread will start and emit the started signal, your object will receive it and process it in its slot/function run().

Note: your thread does not end when the function/slot run() inside your object ends (so you do not need to do a "forever" loop). The thread only stops when you tell it to quit (or destroy it), this means your thread can be idle until it receives a signal or event - this is where the event loop comes in - incoming events are handled by the event loop within the QThread class.

Note: also this code is a snippet - it does not deal with the shutting down of the thread, there are other "template" bits of code that you can use for this.

Edit

So events are handled by the event queue (things like mouse click events all of base type QEvent) - used more by the system where some events may trigger signals (onClicked for example). Signals and slots are a different mechanism that is used more by the user where you handle these in your slots using the connect() function. Here is a better explanation then I could come up with: see here

like image 56
code_fodder Avatar answered Nov 16 '22 23:11

code_fodder


Start reading here for some basic information and here for the complete information.

like image 2
hmuelner Avatar answered Nov 17 '22 00:11

hmuelner