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. Threads problems
Forum Updated to NodeBB v4.3 + New Features

Threads problems

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 2 Posters 1.5k Views 2 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.
  • V Offline
    V Offline
    VRonin
    wrote on 14 Aug 2018, 13:42 last edited by VRonin
    #4

    Are you calling get() on the future returned by std::async? that's what freezes the GUI.

    You can change it to:

    QObject* finishObject= new QObject;
    QObject::connect(finishObject,&QObject::destroyed,[](){std::cout << "unzip_v2 finished";});
    std::async(std::launch::async,[finishObject](QByteArray filename){unzip_v2(filename.constData()); delete finishObject;},filename);
    

    So that your GUI does not stop and you have control on when the process actually finished.
    I used QByteArray so that you don't have to worry about the lifetime of the argument passed to unzip_v2

    One last note: never change your GUI directly from a secondary thread

    "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
      DoubleC122
      wrote on 14 Aug 2018, 14:31 last edited by
      #5

      Hm, doesn't seem to work. My unzip function is called from a signal function though, could that mean something related to the gui freezing?

      1 Reply Last reply
      0
      • V Offline
        V Offline
        VRonin
        wrote on 14 Aug 2018, 14:35 last edited by
        #6

        The code above is GUI-less so it's hard to guess without knowing what your code looks like

        "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
          DoubleC122
          wrote on 14 Aug 2018, 14:51 last edited by DoubleC122
          #7

          Well it works like this. First, I have a signal called on_listView_customContextMenuRequested(const QPoint &pos) which will pop a context menu on right click. Then, based on what I select, I'll call the function that actually pops the menu, showContextMenu(pos). There, I assemble my menu with things like: myMenu.addAction("Unzip here") and other options. I guess it's worth to mention that I use QSignalMapper to connect some slots that have arguments (yes, I know it's deprecated, but I found that this way worked fine for me, might this actually be the issue? :/ ) and then connect everything like this:

          connect(action, SIGNAL(triggered(bool)), mapper, SLOT(map()));
          connect(mapper, SIGNAL(mapped(QString)), this, SLOT(unzip(QString)));
          mapper->setMapping(action, Fpath);
          

          And finally, in the unzip slot I call the unzip_v2 function. If there is the need of more explicit code, I'll post it, didn't want to make this reply too long. And also in the unzip slot is where I use the std::async.

          1 Reply Last reply
          0
          • V Offline
            V Offline
            VRonin
            wrote on 14 Aug 2018, 15:26 last edited by VRonin
            #8

            What I think it's happening is that the QString argument that you pass to unzip_v2 as const char* goes out of scope before unzip_v2 completed. could you show us the content of unzip?

            P.S.
            QSignalMapper is not the problem here. If you really want to get rid of it you can replace

            connect(action, SIGNAL(triggered(bool)), mapper, SLOT(map()));
            connect(mapper, SIGNAL(mapped(QString)), this, SLOT(unzip(QString)));
            mapper->setMapping(action, Fpath);
            

            with connect(action,&QAction::triggered,this,std::bind(&MyClass::unzip,this,Fpath)); (where MyClass is the type of *this)

            "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

            V 1 Reply Last reply 14 Aug 2018, 15:55
            1
            • D Offline
              D Offline
              DoubleC122
              wrote on 14 Aug 2018, 15:43 last edited by DoubleC122
              #9

              Here is unzip:

              void MainWindow::unzip(QString filePath)
              {
              	
                  boost::filesystem::path tempP(filePath.toStdString());
                  chdir(tempP.parent_path().string().c_str());
                  std::async(std::launch::async,&zipUtils::unzip_v2, tempP.filename().string().c_str()).get();
              }
              
              1 Reply Last reply
              0
              • V VRonin
                14 Aug 2018, 15:26

                What I think it's happening is that the QString argument that you pass to unzip_v2 as const char* goes out of scope before unzip_v2 completed. could you show us the content of unzip?

                P.S.
                QSignalMapper is not the problem here. If you really want to get rid of it you can replace

                connect(action, SIGNAL(triggered(bool)), mapper, SLOT(map()));
                connect(mapper, SIGNAL(mapped(QString)), this, SLOT(unzip(QString)));
                mapper->setMapping(action, Fpath);
                

                with connect(action,&QAction::triggered,this,std::bind(&MyClass::unzip,this,Fpath)); (where MyClass is the type of *this)

                V Offline
                V Offline
                VRonin
                wrote on 14 Aug 2018, 15:55 last edited by VRonin
                #10

                Does this code even compile? is zipUtils a namespace?

                Try changing std::async(std::launch::async,&zipUtils::unzip_v2, tempP.filename().string().c_str()).get();
                to std::async(std::launch::async,[](std::string nameString){zipUtils::unzip_v2(nameString.c_str());}, tempP.filename().string());

                "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
                  DoubleC122
                  wrote on 14 Aug 2018, 16:05 last edited by
                  #11

                  Yes, it does compile just fine and indeed, zipUtils is just a namespace. Even after changing those, it still continues to freeze. This problem seems to keep bugging me for a while now.

                  V 1 Reply Last reply 14 Aug 2018, 16:07
                  0
                  • D DoubleC122
                    14 Aug 2018, 16:05

                    Yes, it does compile just fine and indeed, zipUtils is just a namespace. Even after changing those, it still continues to freeze. This problem seems to keep bugging me for a while now.

                    V Offline
                    V Offline
                    VRonin
                    wrote on 14 Aug 2018, 16:07 last edited by
                    #12

                    @DoubleC122 said in Threads problems:

                    it still continues to freeze.

                    Did you notice I'm not calling get() at the end? that's the prime suspect for the freeze, did you remove it as well?

                    "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
                      DoubleC122
                      wrote on 14 Aug 2018, 16:13 last edited by
                      #13

                      I actually added it at the end since I noticed it wasn't there.

                      V 1 Reply Last reply 14 Aug 2018, 16:15
                      0
                      • D DoubleC122
                        14 Aug 2018, 16:13

                        I actually added it at the end since I noticed it wasn't there.

                        V Offline
                        V Offline
                        VRonin
                        wrote on 14 Aug 2018, 16:15 last edited by VRonin
                        #14

                        Yep, remove it. the fact that was missing was intentional.

                        To explain a bit more, I'm using a lambda here to prevent the data pointed by c_str() going out of scope

                        "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
                          DoubleC122
                          wrote on 14 Aug 2018, 16:21 last edited by
                          #15

                          I tested it right now once again but still won't unfreeze. I know that that for(;;) in unzip_v2 is quite heavy to compute with a large zip but that is why I thought it would get better with threads.

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            DoubleC122
                            wrote on 15 Aug 2018, 12:21 last edited by
                            #16

                            I guess I found out a way to do this. It was just by chance, but, based on what you wrote here, I thought I'd try and use a lambda expression with QThread and QtConcurrent and to my surprise both worked, and I stuck with QThread in the end. The result looks like this:

                            QThread* thread = QThread::create([](std::string nameString){zipUtils::unzip_v2(nameString.c_str());}, tempP.filename().string());
                                    thread->start();
                            

                            This won't freeze the gui anymore and performance is alright (although from time to time the system seems to struggle a bit, maybe it's something I can fix later). Anyway, thank you for your help, without this whole insight I would still be stuck :).

                            1 Reply Last reply
                            0

                            13/16

                            14 Aug 2018, 16:13

                            • Login

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