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
Forum Update on Monday, May 27th 2025

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.
  • V Offline
    V Offline
    VRonin
    wrote on 3 Jan 2017, 08:47 last edited by VRonin 1 Mar 2017, 08:49
    #22

    You can achieve the same with C++11 std::bind:

    QString concatenate(const QString& prefix,const QString& val,const QString& suffix){return prefix+val+suffix;}
    QtConcurrent::mapped(list_of_names,std::bind(concatenate,"Prefix ",std::placeholders::_1," Suffix"));
    

    How to get the return from each one now?
    [...]
    example.result() is returning only "Hello john doe".

    use results() instead of result()

    "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 3 Jan 2017, 09:02 last edited by
      #23

      It's working nicely, but one last question; Is it GUI blocking? I tried to use a for loop inside of the function and it's now showing anything until it's finished.

      V 1 Reply Last reply 3 Jan 2017, 09:12
      0
      • D Defohin
        3 Jan 2017, 09:02

        It's working nicely, but one last question; Is it GUI blocking? I tried to use a for loop inside of the function and it's now showing anything until it's finished.

        V Offline
        V Offline
        VRonin
        wrote on 3 Jan 2017, 09:12 last edited by VRonin 1 Mar 2017, 09:13
        #24

        @Defohin said in Concurrent map equivalent:

        Is it GUI blocking?

        no, unless you force it to be

        I tried to use a for loop inside of the function

        could you post the code?

        "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
        0
        • D Offline
          D Offline
          Defohin
          wrote on 3 Jan 2017, 09:20 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
          • V Offline
            V Offline
            VRonin
            wrote on 3 Jan 2017, 09:53 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 3 Jan 2017, 09:56
            0
            • V VRonin
              3 Jan 2017, 09:53

              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 3 Jan 2017, 09:56 last edited by
              #27

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

              V 1 Reply Last reply 3 Jan 2017, 09:57
              0
              • D Defohin
                3 Jan 2017, 09:56

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

                V Offline
                V Offline
                VRonin
                wrote on 3 Jan 2017, 09:57 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 3 Jan 2017, 09:59 last edited by
                  #29

                  You are a god, thank you.

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    VRonin
                    wrote on 3 Jan 2017, 12:09 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 3 Jan 2017, 15:17 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
                      • V Offline
                        V Offline
                        VRonin
                        wrote on 3 Jan 2017, 15:36 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 3 Jan 2017, 15:46
                        0
                        • V VRonin
                          3 Jan 2017, 15:36

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

                          D Offline
                          D Offline
                          Defohin
                          wrote on 3 Jan 2017, 15:46 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
                          • V Offline
                            V Offline
                            VRonin
                            wrote on 3 Jan 2017, 16:06 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 3 Jan 2017, 16:23
                            2
                            • V VRonin
                              3 Jan 2017, 16:06
                              • 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 3 Jan 2017, 16:23 last edited by
                              #35

                              @VRonin Thank you very much.

                              1 Reply Last reply
                              0

                              31/35

                              3 Jan 2017, 15:17

                              • Login

                              • Login or register to search.
                              31 out of 35
                              • First post
                                31/35
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved