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. update() called from different threads, want paintEvent() to run different code depending on update() arguments
Forum Updated to NodeBB v4.3 + New Features

update() called from different threads, want paintEvent() to run different code depending on update() arguments

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 2.7k Views 3 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.
  • StuckInALoopS Offline
    StuckInALoopS Offline
    StuckInALoop
    wrote on last edited by
    #1

    Hi there,

    I am new to programming, C++ and very new to Qt.

    I am writing a multithreaded program and am having program crashes at runtime. Usually before the program crashes I am given the error "*** glibc detected *** ./blah: double free or corruption (fasttop). I believe this is because I am using a multithreaded program, and am using the update() function on the main program window from both threads, and as the threads are running as fast as possible, the update() function may be called at the same time and both threads will be trying to access the same data in the paintEvent.

    I think this is fixable (in some way or another) because I am calling update() with different arguments to specify the region I want updated (there are 2 pixmaps that get painted on the same window widget next to each other). i.e in the GUI thread I am calling update(10, 10, 780, 610) and in the other thread I am calling update (810, 10, 780, 610). Do arguments to update() get passed to the PaintEvent? If so I could check what the x-coordinate argument in the update() command is from within the paint event and then use an if statement to check whether the x-coordinate corresponds to the left or right pixmap and execute their respective painting code.

    This may not be the way the update() function works, and if so, could anyone suggest a better solution?

    Any help would be greatly appreciated.

    Thanks.

    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Never modify UI from other threads than main thread (where the UI is living)! Qt GUI is NOT thread safe!
      You can emit signals from your threads and connect slots to them in the main thread where you can modify the UI.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      StuckInALoopS kshegunovK 2 Replies Last reply
      3
      • jsulmJ jsulm

        Never modify UI from other threads than main thread (where the UI is living)! Qt GUI is NOT thread safe!
        You can emit signals from your threads and connect slots to them in the main thread where you can modify the UI.

        StuckInALoopS Offline
        StuckInALoopS Offline
        StuckInALoop
        wrote on last edited by StuckInALoop
        #3

        @jsulm Thanks for your reply. I am new to signals and slots so would have to find out how they work and how to implement the signal in the pthread. What would the slot in the Window class be? Would it just be a function which calls update(x, y, w, h)? Is the update() function able to be used as a slot directly?

        EDIT: Also I am accessing variables from another thread. The thread is created with a pointer to the instance of the Window class and I am using variables like this: window->a;
        I assume this is also bad, can this be solved with signals and slots also?

        Thanks again.

        jsulmJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi and welcome to devnet,

          Take look at the Signals & Slots documentation. It should help you get started.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • StuckInALoopS StuckInALoop

            @jsulm Thanks for your reply. I am new to signals and slots so would have to find out how they work and how to implement the signal in the pthread. What would the slot in the Window class be? Would it just be a function which calls update(x, y, w, h)? Is the update() function able to be used as a slot directly?

            EDIT: Also I am accessing variables from another thread. The thread is created with a pointer to the instance of the Window class and I am using variables like this: window->a;
            I assume this is also bad, can this be solved with signals and slots also?

            Thanks again.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @StuckInALoop Regarding accessing variables from other classes: it depends on how you access them. Do you write them? If it is only read access then you do not have to serialize the access. If you read/write then you have to make sure that you do not get race conditions.

            In general it is not a good idea to access those main window parts from other threads directly. It would be better to pass all needed data on thread initialization or via signals/slots.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • jsulmJ jsulm

              Never modify UI from other threads than main thread (where the UI is living)! Qt GUI is NOT thread safe!
              You can emit signals from your threads and connect slots to them in the main thread where you can modify the UI.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by kshegunov
              #6

              @StuckInALoop

              Do arguments to update() get passed to the PaintEvent?

              Yes they do, but the events might get compressed - the paint event will be called with a region that's a union of all the update calls' regions (that were placed before the paint event).

              I am writing a multithreaded program and am having program crashes at runtime. Usually before the program crashes I am given the error "*** glibc detected *** ./blah: double free or corruption (fasttop). I believe this is because I am using a multithreaded program, and am using the update() function on the main program window from both threads, and as the threads are running as fast as possible, the update() function may be called at the same time and both threads will be trying to access the same data in the paintEvent.

              Yes, you most probably have a race condition.

              Thanks for your reply. I am new to signals and slots so would have to find out how they work and how to implement the signal in the pthread.

              You can't have signals in a pthread, Qt signals and slots are bound to Qt, so you should use QThread instead.

              What would the slot in the Window class be? Would it just be a function which calls update(x, y, w, h)?

              It would.

              Is the update() function able to be used as a slot directly?

              Depends on the syntax used (Qt 4 or Qt 5).
              But assume the answer is: Yes, but only the overload that doesn't take any arguments.

              I assume this is also bad, can this be solved with signals and slots also?

              It is (most of the time) and it can if your Window class derives from QObject, if it doesn't then you can't use signal and slots. Your variables can be read from any thread (as @jsulm duly noted), however you must ensure that no thread will modify them in the meantime (which you can't if the base class is QWidget and the variables belong to that base).

              @jsulm said:

              Qt GUI is NOT thread safe!

              Sadly, it's not even reentrant ... :|

              Read and abide by the Qt Code of Conduct

              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