Results 1 to 5 of 5

Thread: Please critique: solution to database connection and multiple threads

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Please critique: solution to database connection and multiple threads

    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:

    Qt Code:
    1. void Controller::onNotification(const Notification *notification, void *caller) {
    2. Controller *ref = static_cast<Controller *>(caller);
    3. NotificationWrapper wrapper(notification);
    4. QMetaObject::invokeMethod(ref, "doStuff", Qt::AutoConnection, Q_ARG(NotificationWrapper, wrapper));
    5. }
    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.
    Last edited by Urthas; 21st October 2015 at 21:47.

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Please critique: solution to database connection and multiple threads

    This is coming a bit late, especially since you have moved away from your one-connection-per-thread solution, but if you ever have to do something like that again, consider using thread-local storage (see QThreadStorage for Qt's implementation) instead of rolling out your own map protected by a mutex. Thread-local storage is supported by the C11 and C++11 standards, and is usually natively supported by the OS. IIRC a typical implementation consists in reserving a CPU register that points to a different memory area for each thread.

  3. The following user says thank you to yeye_olive for this useful post:

    Urthas (22nd October 2015)

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. SignalSlot Connection across the threads
    By shruti in forum Newbie
    Replies: 9
    Last Post: 29th March 2013, 10:30
  3. Replies: 5
    Last Post: 20th January 2013, 18:06
  4. Replies: 0
    Last Post: 21st April 2010, 19:13
  5. SQLite connection shared by several threads
    By jorgegarciar in forum Qt Programming
    Replies: 1
    Last Post: 23rd August 2009, 20:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.