Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMetaCallEvent in multithreading application
Forum Updated to NodeBB v4.3 + New Features

QMetaCallEvent in multithreading application

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arykov
    wrote on last edited by
    #1

    Hi there,

    We have an application that is running GUI in the main thread T1 and there are other threads T2-Tx that are making calls to objects in T1 thread.

    @class QMyApp : public QApplication
    {
    ...
    }
    @

    T1 is running event loop:

    @bool QApplication::notify(QObject* receiver, QEvent* event)
    {
    try
    {
    return QApplication::notify(receiver, event);
    }
    catch (std::exception& exception)
    {
    qFatal("Error '%s' sending event '%s' to object '%s' ('%s').", exception.what(), typeid(*event).name(), qPrintable(receiver->objectName()), typeid(*receiver).name());
    throw;
    }
    catch (...)
    {
    qFatal("Error <unknown> sending event '%s' to object '%s' ('%s').", typeid(*event).name(), qPrintable(receiver->objectName()), typeid(*receiver).name());
    throw;
    }
    return false;
    }@

    T2 is sending event to T1:

    QMetaObject::invokeMethod(obj, member, Qt::BlockingQueuedConnection, Q_RETURN_ARG(QString, result), Q_ARG(QString, qParam1), Q_ARG(QString, qParam2), Q_ARG(QString, qParam3));

    T2 waits while T1 processes the event and returns the response.

    The issues is that sometimes we have AV exception in QApplication::notify() method (I think because sometimes receiver is dead ath the moment of event processing). So, T1 caught an exception and tries to close application with qFatal, but T2 is waiting when QMetaCallEvent will be destroyed (and QSemaphore will be released). Is it okay to destroy event manually (delete event) in catch block?

    The goal is to close application and avoid hangs.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rcari
      wrote on last edited by
      #2

      I think you have a design problem.
      You must ensure that T2 is not sending requests to T1 anymore when you destroy it. I think the simplest solution for you would be to simply use signals and slots for communication between T2 and T1 as what happens in the background is pretty much what you are doing by hand.
      However, the signal/slot mechanism provides extra security in the sense that when one object is destroyed (using deleteLater()) all its pending events are processed and all its signals/slots are cleanly disconnected.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        arykov
        wrote on last edited by
        #3

        Thank you for your response! We definitely have problem with architecture there. We are looking for appropriate solution. But as temporary hack -- it seems it's okay to destroy QMetaCallEvent in the catch block.

        [quote author="rcari" date="1349775603"]I think you have a design problem.
        You must ensure that T2 is not sending requests to T1 anymore when you destroy it. I think the simplest solution for you would be to simply use signals and slots for communication between T2 and T1 as what happens in the background is pretty much what you are doing by hand.
        However, the signal/slot mechanism provides extra security in the sense that when one object is destroyed (using deleteLater()) all its pending events are processed and all its signals/slots are cleanly disconnected.[/quote]

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved