Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

in below function exactly where i should put QMutex lock and unlock ?



  • int FuelCheck::gauge_write(int &Fd_file, unsigned char nRegister, unsigned char *pData, unsigned char nLength)
    {
        unsigned char pWriteData[nLength+ 1];
        int n = 0;
    
        if (nLength< 1)
        {
    #ifdef QDEBUG_ENABLE
            pMainApp.ObjSettings.DebugStation("Fuel gauge read data length is less than 1 ");
    #endif
            return 0;
        }
        pWriteData[0]= nRegister;
        // write register address before writing data
        memcpy(pWriteData+ 1, pData, nLength);
    
        n = write(Fd_file, pWriteData, nLength+ 1);
    
        usleep(200);
    
        if(n == -1)
        {
    #ifdef QDEBUG_ENABLE
            pMainApp.ObjSettings.DebugStation("Failed to write fuel gauge data");
    #endif
        }
    
        return (n-1);
    }
    


  • @Qt-embedded-developer
    Yes, In the code as presently shown the minimum you would need to protect are the two lines:

        memcpy(pWriteData+ 1, pData, nLength);
        n = write(Fd_file, pWriteData, nLength+ 1);
    

    for their use of pData & Fd_file. I don't know whether your pMainApp.ObjSettings.DebugStation() calls need protection too.

    Separately I don't know whether your system call to write(2) safely allows writing from separate threads, that is for you to check.


  • Lifetime Qt Champion

    Hi,

    Around the part(s) that access shared data.



  • Hi my Fd_file is file descriptor which shared at multiple places so

    i need to use mutex. lock above it where it get accessed and mutex.unlock below it .

    am i right ?



  • @Qt-embedded-developer
    Yes, In the code as presently shown the minimum you would need to protect are the two lines:

        memcpy(pWriteData+ 1, pData, nLength);
        n = write(Fd_file, pWriteData, nLength+ 1);
    

    for their use of pData & Fd_file. I don't know whether your pMainApp.ObjSettings.DebugStation() calls need protection too.

    Separately I don't know whether your system call to write(2) safely allows writing from separate threads, that is for you to check.


Log in to reply