is the qdatastream thread safe or not ?
-
I just used the QJsonObject in the new std::thread .and the heob debugger give me a tip:
<unique>0x000860F2</unique> <tid>26</tid> <kind>Leak_DefinitelyLost</kind> <xwhat> <text>160816 bytes in 1 blocks are lost in loss record 12 of 81505 (#549106)</text> <leakedbytes>160816</leakedbytes> <leakedblocks>1</leakedblocks> </xwhat> <stack> <frame> <fn>malloc</fn> </frame> <frame> <ip>0x673B1B17</ip> <obj>D:\program\qt\5.14.1\msvc2017\bin\Qt5Cored.dll</obj> <fn>class QDataStream & operator>>(class QDataStream &,class QJsonObject &)</fn> </frame> <frame> <ip>0x673AEDD2</ip> <obj>D:\program\qt\5.14.1\msvc2017\bin\Qt5Cored.dll</obj> <fn>bool QJsonObject::detach2(unsigned int)</fn> </frame> <frame> <ip>0x673B09F3</ip> <obj>D:\program\qt\5.14.1\msvc2017\bin\Qt5Cored.dll</obj> <fn>class QDataStream & operator>>(class QDataStream &,class QJsonObject &)</fn> </frame> <frame> <ip>0x673B0D92</ip> <obj>D:\program\qt\5.14.1\msvc2017\bin\Qt5Cored.dll</obj> <fn>class QDataStream & operator>>(class QDataStream &,class QJsonObject &)</fn> </frame> <frame> <ip>0x673AEA5E</ip> <obj>D:\program\qt\5.14.1\msvc2017\bin\Qt5Cored.dll</obj> <fn>class QJsonObject::iterator QJsonObject::insert(class QStringView,class QJsonValue const &)</fn> </frame> <frame> <ip>0x673AE96B</ip> <obj>D:\program\qt\5.14.1\msvc2017\bin\Qt5Cored.dll</obj> <fn>class QJsonObject::iterator QJsonObject::insert(class QString const &,class QJsonValue const &)</fn> </frame>I just use the std::mutex to lock the data write and read.
so I think maybe its not safe with using the std::mutex to read and write the qjsonobject? dose i need to change the std::mutex into the QMutex ? -
No Qt object or function is thread safe except it is explicitly stated in the documentation.
-
so if I used the std::thread and the std::mutex to lock the data.what should I do the make sure that writting and reading the data is safe .
class A:QObject{ .... private: QJsonObject t_data; public: void writing(const QString & value){ t_data.insert("data",value); do_otherthings_such_as_update_the_ui_value(); } } //std thread functions what is detached from the main thread. int testfunction(){ //heres to modify and send the data into the writing method. A aobject; aobject.writing("somevalue"); }so dose i need to lock the the t_data by using the QMutex or using the emit method to to the other data what is in the main thread instead of using the lock way.what should i do.please.
-
When you only access an object from a single thread why do you ask about thread safeness and mutexes here?
-
In fact.
I wrote the data in a new std::thread and then post the result to the qt ui main loop.
Now I just used the emit way to send the data to the ui loop.