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. Concurrent map equivalent
QtWS25 Last Chance

Concurrent map equivalent

Scheduled Pinned Locked Moved Solved General and Desktop
35 Posts 5 Posters 9.7k Views
  • 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.
  • D Offline
    D Offline
    Defohin
    wrote on last edited by
    #25
    struct NameHelper
    {
        NameHelper(const QString &extra) : _extra(extra) { }
    
        typedef QString result_type;
    
        QString operator()(const QString &name)
        {
            for(int i = 0; i < 999999; ++i) {
                qDebug() << "block";
            }
    
            return QString("Hello %1 %2").arg(name).arg(_extra);
        }
    
        QString _extra;
    };
    
    QStringList names = { "john", "jane" };
    QString extra = "doe";
    
    QFuture<QString> example = QtConcurrent::mapped(names, NameHelper(extra));
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #26

      operator() will be executed in another thread, it will not block the GUI thread, it will not return until completed (of course)

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      D 1 Reply Last reply
      0
      • VRoninV VRonin

        operator() will be executed in another thread, it will not block the GUI thread, it will not return until completed (of course)

        D Offline
        D Offline
        Defohin
        wrote on last edited by
        #27

        @VRonin But why is it not appearing the window If it's running in another thread?

        VRoninV 1 Reply Last reply
        0
        • D Defohin

          @VRonin But why is it not appearing the window If it's running in another thread?

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #28

          @Defohin said in Concurrent map equivalent:

          But why is it not appearing the window If it's running in another thread?

          WOW! we are taking this on a whole new level here! you need a QFutureWatcher on the QFuture and a slot connected to the resultReadyAt signal to display the results in the GUI

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2
          • D Offline
            D Offline
            Defohin
            wrote on last edited by
            #29

            You are a god, thank you.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #30

              I definitely am not, trust me.

              Just as a final remark, in your case, if you can use C++11, I'd use std::bind over the function object

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              1
              • D Offline
                D Offline
                Defohin
                wrote on last edited by
                #31

                I'm using std::bind now... I realized something:
                THe threads is not closing if the application closes, meaning that if I close the window it will still open. (if I do a heavy work inside the called function).

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #32

                  Since you are using mapped you can call cancel() to signal to abort the calculation

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  D 1 Reply Last reply
                  0
                  • VRoninV VRonin

                    Since you are using mapped you can call cancel() to signal to abort the calculation

                    D Offline
                    D Offline
                    Defohin
                    wrote on last edited by
                    #33

                    @VRonin Do I have to connect that to a signal from the window for when it's closing or something?

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #34
                      • If you want to stop the calculation when the widget is closed reimplement the closeEvent and call cancel on the QFuture
                      • If you want to stop the calculation when the widget is destroyed reimplement the destructor and call cancel on the QFuture

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      D 1 Reply Last reply
                      2
                      • VRoninV VRonin
                        • If you want to stop the calculation when the widget is closed reimplement the closeEvent and call cancel on the QFuture
                        • If you want to stop the calculation when the widget is destroyed reimplement the destructor and call cancel on the QFuture
                        D Offline
                        D Offline
                        Defohin
                        wrote on last edited by
                        #35

                        @VRonin Thank you very much.

                        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