[Development] Execute a function in the specified existing thread

JiDe Zhang zccrs at live.com
Wed Jun 7 10:53:47 CEST 2023


Hi,

I am writing a tool to allowed call a function in the specified existing thread, I want to contribution it to Qt project, are you like?

We have such needs in many places, such as QQmlThreadPrivate::triggerMainEvent(https://github.com/qt/qtdeclarative/blob/dev/src/qml/qml/ftw/qqmlthread.cpp#L68).
generally using a custom event to call the function in the event processing, but the code implemented in this way is not easy to read, maybe we can encapsulate
a common interface to provide this function. I implemented a demo that allows us to use it like the following code (full code is at: https://github.com/zccrs/qt-demo/tree/main):

int main(int argc, char *argv[])
{
     QCoreApplication a(argc, argv);

     a. thread()->setObjectName("MainThread");

     QThread testThread;
     testThread.setObjectName("TestThread");
     testThread. start();

     // Call In Test Thread
     auto future = QtConcurrent::run(&testThread, function);
     future. then(&a, [] () {
         qDebug() << QThread::currentThread(); // Main Thread
     });

     return a.exec();
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20230607/67ed083d/attachment.htm>


More information about the Development mailing list