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. May need help with GUI + Multi Threading
Forum Updated to NodeBB v4.3 + New Features

May need help with GUI + Multi Threading

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 837 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.
  • M Offline
    M Offline
    MaKoZer
    wrote on last edited by MaKoZer
    #1

    Hello,
    what i want to do:
    GUI has a Class XYZ with a QVector, that Class creates ℕ threads with another instance of that same class, all have a pointer to their "creator" class and they are running an algorithm to search for a solution. if one of the threads finds the solution, it gives to the "creator" class a QVector with the solution in it and then the GUI should update.

    maybe better to understand workflow:

    1. User fills a QVector with numbers
    2. clicks "start calculate" ( important: gui shouldnt freeze )
    3. "XYZ creator" creates ℕ threads and starts them to search for the solution
    4. if one of the threads finds the solution, it will give it back the solution as QVector to the creator
    5. all other threads stop running cuz solution was found
    6. GUI reads from the creator and updates and displays the solution

    i already looked to the example of the mandelbrot but it didnt really helped me :/
    actually its just the GUI without multithreading and its freezing while it searches ....
    im reading the threading stuff but im struggling to find the right way ...

    does someone has good advices for me?

    would be very happy about good advices to start this :)

    greetings :)

    JonBJ M 2 Replies Last reply
    0
    • M MaKoZer

      Hello,
      what i want to do:
      GUI has a Class XYZ with a QVector, that Class creates ℕ threads with another instance of that same class, all have a pointer to their "creator" class and they are running an algorithm to search for a solution. if one of the threads finds the solution, it gives to the "creator" class a QVector with the solution in it and then the GUI should update.

      maybe better to understand workflow:

      1. User fills a QVector with numbers
      2. clicks "start calculate" ( important: gui shouldnt freeze )
      3. "XYZ creator" creates ℕ threads and starts them to search for the solution
      4. if one of the threads finds the solution, it will give it back the solution as QVector to the creator
      5. all other threads stop running cuz solution was found
      6. GUI reads from the creator and updates and displays the solution

      i already looked to the example of the mandelbrot but it didnt really helped me :/
      actually its just the GUI without multithreading and its freezing while it searches ....
      im reading the threading stuff but im struggling to find the right way ...

      does someone has good advices for me?

      would be very happy about good advices to start this :)

      greetings :)

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @MaKoZer
      Keep all your computation threads separate from the main GUI thread. A thread which finds a solution can emit a signal telling the creator-thread it's been found, and that can terminate the other threads.

      I think all the sub-threads can read the creator-thread's QVector OK (no updating or going out of scope). Not sure how you pass a QVector with the answer back from a thread. QSharedPointer can be used if needed.

      M 1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        If you pass around your creator object, then you have to properly protect your access to it. The easiest is to leverage Qt's signal and slot mechanism like shown in the mandelbrot example. As for stoping the other thread, depending on your algorithm, you should have an exit point you check from time to time because "terminating" threads is not the best way to stop them.

        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
        1
        • JonBJ JonB

          @MaKoZer
          Keep all your computation threads separate from the main GUI thread. A thread which finds a solution can emit a signal telling the creator-thread it's been found, and that can terminate the other threads.

          I think all the sub-threads can read the creator-thread's QVector OK (no updating or going out of scope). Not sure how you pass a QVector with the answer back from a thread. QSharedPointer can be used if needed.

          M Offline
          M Offline
          MaKoZer
          wrote on last edited by
          #4

          @JonB
          yeah but how do i do this if the GUI is the start point and the GUI starts everything else? x.X

          but ill keep working on this

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

            How do you do what ?

            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
            2
            • M MaKoZer

              Hello,
              what i want to do:
              GUI has a Class XYZ with a QVector, that Class creates ℕ threads with another instance of that same class, all have a pointer to their "creator" class and they are running an algorithm to search for a solution. if one of the threads finds the solution, it gives to the "creator" class a QVector with the solution in it and then the GUI should update.

              maybe better to understand workflow:

              1. User fills a QVector with numbers
              2. clicks "start calculate" ( important: gui shouldnt freeze )
              3. "XYZ creator" creates ℕ threads and starts them to search for the solution
              4. if one of the threads finds the solution, it will give it back the solution as QVector to the creator
              5. all other threads stop running cuz solution was found
              6. GUI reads from the creator and updates and displays the solution

              i already looked to the example of the mandelbrot but it didnt really helped me :/
              actually its just the GUI without multithreading and its freezing while it searches ....
              im reading the threading stuff but im struggling to find the right way ...

              does someone has good advices for me?

              would be very happy about good advices to start this :)

              greetings :)

              M Offline
              M Offline
              MaKoZer
              wrote on last edited by MaKoZer
              #6

              Ok if someone finds this thread with the same problem:

              Your Class needs to inherit from QThread.
              Class has private:
              QVector<Class*> workers;
              QVector<QFuture<void>> qfs;

              Method to start MT in your class:

              void mt_search(Class &worker) {
                  class.startYourCalculations();
              }
              

              Method to start:

              bool Class::runCalc() {
                for (quint8 i = 0; i < num_threads; i++) {
                      workers.push_back(new Class());
                      workers[i]->setSomething(something);
                      qfs.push_back(QtConcurrent::run(mt_search, *workers[i]));
                }
              }
              

              works pretty fine now :)

              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