Linux Qt 5 C++ std::vector struct implicit sharing
-
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
-
@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 useQVector
. Note that this ain't a one-size-fits-all though, asQVector
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.
-
@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 -
@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 thanstd::vector
. -
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
-
@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 ....
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