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. Write/read to the same object, multiThreading
QtWS25 Last Chance

Write/read to the same object, multiThreading

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.8k 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.
  • K Offline
    K Offline
    kyoti
    wrote on last edited by
    #1

    I have developed a QT application that connects to two micro-controllers (two threads), and whenever data are received from the serial ports
    each thread emit a signal to update the value of certain objects (all of them are QString) in the GUI class. now the thing is that I need to write the latest values of these data on a single file every 20 ms from the GUI class. I have done all that and it seems that its working fine, but what if the GUI class wanted to
    write a value of these objects to the file and at the same time one of the threads was updating the value of that same object??
    how can I address this problem.....or is it already covered by QT

    thanks in advance

    this is an example of what I am doing:

    @
    Class A{

    void dataReceived(QByteArray a){
    QString s = a.at[1];
    emit updateGUI(s);
    }
    }

    Class GUI{
    ......
    QString ss;

    void updateGUI(QString s){
    ss = s;
    }

    void writeToFile(){

    fileWriter->writeToFile(ss);
    }

    }
    @

    what if writeToFile() and updateGUI were called at the same time, then one thread will update the value of ss where the other will read the value of ss...is
    that ok?

    [EDIT: code formatting, please wrap in @-tags, Volker]

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hostel
      wrote on last edited by
      #2

      I think that you need a QMutex in Class GUI.
      "QMutex doc":http://doc.qt.nokia.com/latest/qmutex.html
      Read a link - there is example. Read also about QMutexLocker.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kyoti
        wrote on last edited by
        #3

        thank you for your reply, but how to use is? one of the threads is always writing(so it only calls updateGUI) where the other
        is always reading the object ss (so it just calls writeToFile())

        so how to give the writing more priority than the reading of the object NOT the functions. the other
        thing is one of the threads is the GUI thread (will let go to sleep??)

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Hostel
          wrote on last edited by
          #4

          I would try something like this(but I don't know for a 100% that is correct):
          in class GUI add a member:
          @
          Class GUI{
          ……
          // add mutex
          QMutex mutex;
          QString ss;

          void updateGUI(QString s){
          // turn on mutex
          QMutexLocker lock( &mutex );
          ss = s;
          }

          void writeToFile(){
          // turn on mutex
          QMutexLocker lock( &mutex);
          fileWriter->writeToFile(ss);
          }

          }
          @

          I think that you will have a guarantee that only one method can work with ss on the same time.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kyoti
            wrote on last edited by
            #5

            thank you very much you
            but don't I need to unlock the mutex after I finish?

            1 Reply Last reply
            0
            • H Offline
              H Offline
              Hostel
              wrote on last edited by
              #6

              A QMutexLocker will unlock a mutex when he will be destroyed.
              "QMutexLocker doc":http://doc.qt.nokia.com/latest/qmutexlocker.html#details

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KA51O
                wrote on last edited by
                #7

                another option may be "QReadWriteLocker":http://doc.qt.nokia.com/4.7/qreadwritelock.html if this is your "problem":http://en.wikipedia.org/wiki/Readers-writers_problem

                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