"printf" appears to be non-deterministic in Qt?

297 views Asked by At

I know "printf" is standard-c and should be deterministic. But when run in Qt I see a more non-deterministic response(clock cycles). Could this be due to Qt adding some "pork" to its response?

I have multiple threads that make call to function that uses a mutex. When one thread enters it set a switch so the others can't until it is done. Things appeared to work ok for acouple seconds and then threads appeared to be killed off from 10 to 1 thread. So I tried adding a delay: (k=k+1: no help), then (looping k=k+1: no help), (usleep works), and so does (printf) work at creating a random delay and allowing all threads to continue running.

void CCB::Write(int iThread)
{
    static bool bUse = false;
    bool bDone = false;
    char cStr[20];
    int posWrite;// = *m_posWrite;  // issue of posWrite be altered with next extrance
    long k = 0;
    long m = 0;
    m_threadCount++;

    while(bDone == false){

        if(bUse == false){
            bUse = true;
            posWrite = *m_posWrite;

            memcpy(m_cmMessageCB + posWrite, &m_cmMessageWrite, sizeof(typeCanMessage));

            memset(cStr, '\0', 20);
            memcpy(cStr, (m_cmMessageCB + posWrite)->cMessage, 11); //fails: every 20

            *m_posWrite = *m_posWrite + 1;
            if(*m_posWrite == m_iNBufferLength)
                *m_posWrite = 0;

            bDone = true;
            bUse = false;

        }else if(bUse == true){
            //why are threads being killed ?
    //            printf("T%d_%d ", iThread, m_threadCount);//non-deterministic value ?
            usleep(1);//non-deterministic value
            //k++;//delay of a couple clock cycles was not enough

            /*
            for(k = 0; k < iThread * 100; k++){//deterministic and fails to resolve thread problem
                m++;
            }
            */
        }
    }
}
0

There are 0 answers