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. Mutex to device simultaneous access

Mutex to device simultaneous access

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 482 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.
  • F Offline
    F Offline
    federico.massimi
    wrote on last edited by
    #1

    Hi, I have a question. I'm creating an application that needs to access a device via the serial port. Communication always takes place by sending a buffer and receiving the response buffer from the device, generally the device responds in about 200 milliseconds.

    Access to the device takes place from a function that is executed periodically by a QML Timer and also by the user through some buttons in the ui. The problem here is how to avoid the situation in which the user tries to access the device at the same time as the Timer function also tries to do it.

    I thought about using a MUTEX, but I'm not an expert and I don't know if it's the right solution, could it work well? Is there a better way to do this?

    In case the mutex was the best solution, the function that accesses the device could be done in this way:

    QByteArray deviceAccess(QByteArray query) {
        serialport_mutex.lock();
    
        QByteArray readDataBuffer = serialPort.readAll();
        while (serialPort.waitForReadyRead(5000))
            readDataBuffer.append(serialPort.readAll());
            if(dataComplete(readDataBuffer)) break;
    
        serialport_mutex.unlock();
    
        return readDataBuffer;
    }
    

    Thanks in advance, Federico

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

      @federico-massimi said in Mutex to device simultaneous access:

      situation in which the user tries to access the device at the same time as the Timer function also tries to do it.

      When you don't use thread this can't happen since then both actions are taking place in the main thread so no problems here.

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

      F 1 Reply Last reply
      3
      • K Offline
        K Offline
        kuzulis
        Qt Champions 2020
        wrote on last edited by kuzulis
        #3

        Don't use the mutexes at all for locking the I/O access for QSerialPort, QAbstractSocket and other QIODevice classes.

        1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          @federico-massimi said in Mutex to device simultaneous access:

          situation in which the user tries to access the device at the same time as the Timer function also tries to do it.

          When you don't use thread this can't happen since then both actions are taking place in the main thread so no problems here.

          F Offline
          F Offline
          federico.massimi
          wrote on last edited by
          #4

          @Christian-Ehrlicher I thought that by using a timer another thread would be generated, so in my case is it impossible that during the execution of the timer callback also the callback of a button (as example) can be executed?

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

            When you don't create a separate thread there won't be any.

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

            F 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              When you don't create a separate thread there won't be any.

              F Offline
              F Offline
              federico.massimi
              wrote on last edited by
              #6

              @Christian-Ehrlicher ok, thanks a lot for the clarification

              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