Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Linux Qt 5 C++ std::vector struct implicit sharing

    General and Desktop
    3
    6
    3329
    Loading More Posts
    • 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.
    • gfxx
      gfxx last edited by A Former User

      I try to do these implicit sharing trought QThread (firt ideas is use connect -signal-slot mechanism on gui thread but I nees something more cheeper)...

      ``/`QThread 1***/

      Send ppoint;  /********struct 1**********/
      SendCoord CPointCoord; /********struct 2**********/
      
      std::vector<Send> Point;
      std::vector<SendCoord> CoordPoint;
      
                                            CPointCoord.CoordX = round(((((ppoint[ckpp].SendY / 10)*KonsTagy) + KonstYoff) * InversionY)*10);
                                            CPointCoord.CoordY = round(((((ppoint[ckpp].SendX / 10)*KonsTagx) + KonstXoff) * InversionX)*10);
                                            CPointCoord.CoordA = round(Point[ckpp].SendA*10);
      
                                            CoordPoint.push_back(CPointCoord);
      
      
                                  QByteArray* SendK1Coord =  new QByteArray(reinterpret_cast<const char*>(&CoordPoint), 80);
                                  emit StructDataCoord(SendK1Coord);
      

      /end**********/

      /QThread 2******/

      void QThread2::recInputQuote(QByteArray *K1quote)
      {

      for (int ricK = 0; ricK < K1quote->size(); ++ricK) 
      {
          int test1 = K1quote->at(ricK);  /************ receive struct "CPointCoord" data ****************/
      
      }
      

      }

      
      any suggest ??
      
      Regards
      Giorgio

      bkt

      kshegunov 1 Reply Last reply Reply Quote 0
      • kshegunov
        kshegunov Moderators @gfxx last edited by kshegunov

        @gfxx
        I have no idea what this code is supposed to do, but to answer the title of the thread - no, std::vector isn't implicitly shared, nor will be (in all probability). It will deep copy whatever it is contained in it, if the instance isn't passed by reference. If you need something cheaper to pass shallow copies around, then use QVector. Note that this ain't a one-size-fits-all though, as QVector will silently detach (make a deep copy) the data it's holding if needed on a non-const method call, so there's that to think about.

        If you intend to pass around coordinates (like it seems you do) declare your coordinate struct/class as "movable" with the metatype system so QVector can actually optimize the data copy.

        Kind regards.

        Read and abide by the Qt Code of Conduct

        gfxx 1 Reply Last reply Reply Quote 2
        • gfxx
          gfxx @kshegunov last edited by

          @kshegunov said in linux QT5 c++ std::vector struct implicit sharing ...:

          If you intend to pass around coordinates (like it seems you do) declare your coordinate struct/class as "movable" with the metatype system so QVector can actually optimize the data copy.

          This is my intent ... so convert my std::vector to QVector .... than I see around the solution of my problem ...(ok the title is not very clear ...).

          Regards
          Giorgio

          bkt

          kshegunov 1 Reply Last reply Reply Quote 0
          • kshegunov
            kshegunov Moderators @gfxx last edited by

            @gfxx said in linux QT5 c++ std::vector struct implicit sharing ...:

            so convert my std::vector to QVector

            Yes, and use the Q_DECLARE_TYPEINFO macro with your coordinates structure (as in the link I provided). But I warn you again, QVector isn't an "always best solution", it fares fine for most cases though. There are some fringe cases where it's unsuitable and it will always be a tad bit less efficient than std::vector.

            Read and abide by the Qt Code of Conduct

            gfxx 1 Reply Last reply Reply Quote 1
            • VRonin
              VRonin last edited by

              std::vector allocates memory on the heap, the reinterpret_cast<const char*> trick (as horrible as it is) can't work.

              Use QVector instead of std::vector and use

              QByteArray SendK1Coord;
              QDataStream SendK1Stream(&SendK1Coord,QIODevice::WriteOnly);
              SendK1Stream << CoordPoint;
              

              to save it to byte array

              you can the use this to read it

              void QThread2::recInputQuote(QByteArray K1quote){
              QVector<SendCoord> CoordPoint;
              QDataStream K1Stream(K1quote);
              K1Stream >> CoordPoint;
              
              // for(...
              }
              
              

              I still don't understand why you pass through a QByteArray though as you could send the QVector directly or declare and register the structs and the std::vectors of structs as metatypes and send them directly

              "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 Reply Quote 3
              • gfxx
                gfxx @kshegunov last edited by

                @kshegunov said in linux QT5 c++ std::vector struct implicit sharing ...:

                There are some fringe cases where it's unsuitable and it will always be a tad bit less efficient than std::vector.

                I think use std::vector ....

                @VRonin

                or declare and register the structs and the std::vectors of structs as metatypes and send them directly
                I never make these .... I try now It seem the good solution for quite good performance ...

                Regards
                Giorgio

                bkt

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post