M
M
Mercury132016-08-08 15:29:42
Qt
Mercury13, 2016-08-08 15:29:42

Qt: How does emit work and under what conditions does it go into multi-threaded mode?

Here we are in the thread constructor (which, of course, is executed from the main thread) we write:

MyThread::MyThread() : mainControl(откуда-то возьмём)
{
  connect(this, MyThread::doSomething, mainControl->asQobject(), [this]() {
    mainControl->doSomething();
  });
}

and further in the flow
void MyThread::run()
{
  emit doSomething();
}

(mainControl is a class field, a regular C++ interface, mainControl->asQobject() is the main form of the program)
Two questions.
1. Will there be synchronization with the main thread in such a situation? Or are there some more interesting methods to synchronize with the main thread?
2. Will the captured this disappear when we got out of the function where the lambda was created a long time ago?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2016-08-08
@Mercury13

1) Look at the last argument of the connect method - this is the answer to your question ( https://doc.qt.io/qt-5/qobject.html#connect-5 )
2) The question is not very clear. The lambda in the connect method works like a regular lambda.
Here *almost* everything is written about multithreading in Qt: https://wiki.qt.io/Threads_Events_QObjects
Here about lambdas: en.cppreference.com/w/cpp/language/lambda
And yes, your code will not compile for many reasons . For example, like this: *this cannot be written before the 17th standard.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question