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. Linux Qt 5 C++ std::vector struct implicit sharing
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 3.7k 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.
  • gfxxG Offline
    gfxxG Offline
    gfxx
    wrote on last edited by A Former User
    #1

    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

    kshegunovK 1 Reply Last reply
    0
    • gfxxG gfxx

      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
      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @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

      gfxxG 1 Reply Last reply
      2
      • kshegunovK 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.

        gfxxG Offline
        gfxxG Offline
        gfxx
        wrote on last edited by
        #3

        @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

        kshegunovK 1 Reply Last reply
        0
        • gfxxG gfxx

          @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

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @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

          gfxxG 1 Reply Last reply
          1
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            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
            3
            • kshegunovK kshegunov

              @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.

              gfxxG Offline
              gfxxG Offline
              gfxx
              wrote on last edited by
              #6

              @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
              0

              • Login

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