how else might one get around it? Are there certain situations in which certain approaches are best?
I'm going to partially answer my own questions, for posterity.
It just so happens that code executed in the sensor thread (which is not created by the application) uses an instance of a custom class that has-a QTimer. This resulted in the following warning:
QObject::startTimer: Timers can only be used with threads started with QThread
To get around this, I wrap the sensor input and pass it to the main thread instead of the lazy-load-and-cache database connections per thread approach:
void Controller::onNotification(const Notification *notification, void *caller) {
Controller *ref = static_cast<Controller *>(caller);
NotificationWrapper wrapper(notification);
QMetaObject::invokeMethod(ref,
"doStuff", Qt
::AutoConnection, Q_ARG
(NotificationWrapper, wrapper
));
}
void Controller::onNotification(const Notification *notification, void *caller) {
Controller *ref = static_cast<Controller *>(caller);
NotificationWrapper wrapper(notification);
QMetaObject::invokeMethod(ref, "doStuff", Qt::AutoConnection, Q_ARG(NotificationWrapper, wrapper));
}
To copy to clipboard, switch view to plain text mode
Note that there is some legwork involved in calling QMetaObject::invokeMethod() with a custom type. See http://doc.qt.io/qt-5/qmetaobject.html#invokeMethod.
Since all the work is now on the main thread, only a single database connection is required. In addition, calls to QMutexLocker that were peppered throughout the code have been removed.
Bookmarks