Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. is the qdatastream thread safe or not ?
Qt 6.12 LTS is out! See what's new in the release blog

is the qdatastream thread safe or not ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.7k Views
  • 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.
  • nicker playerN Offline
    nicker playerN Offline
    nicker player
    wrote on last edited by nicker player
    #1

    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 &amp; operator&gt;&gt;(class QDataStream &amp;,class QJsonObject &amp;)</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 &amp; operator&gt;&gt;(class QDataStream &amp;,class QJsonObject &amp;)</fn>
        </frame>
        <frame>
          <ip>0x673B0D92</ip>
          <obj>D:\program\qt\5.14.1\msvc2017\bin\Qt5Cored.dll</obj>
          <fn>class QDataStream &amp; operator&gt;&gt;(class QDataStream &amp;,class QJsonObject &amp;)</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 &amp;)</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 &amp;,class QJsonValue const &amp;)</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 ?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      No Qt object or function is thread safe except it is explicitly stated in the documentation.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • nicker playerN Offline
        nicker playerN Offline
        nicker player
        wrote on last edited by nicker player
        #3

        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.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          When you only access an object from a single thread why do you ask about thread safeness and mutexes here?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • nicker playerN Offline
            nicker playerN Offline
            nicker player
            wrote on last edited by
            #5

            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.

            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
            • Unsolved