QNX pthread_mutex_lock导致死锁错误(45 = EDEADLK)

时间:2019-01-02 09:54:38

标签: c++ queue mutex deadlock qnx

我正在为我的项目的多线程应用程序实现异步日志写入机制。下面是发生错误的部分的部分代码。

void CTraceFileWriterThread::run() 
{
    bool fShoudIRun = shouldThreadsRun();  //  Some global function which decided if operations need to stop. Not really relevant here. Assume "true" value.

    while(fShoudIRun)
    {
      std::string nextMessage = fetchNext();
      if( !nextMessage.empty() )
      {
          process(nextMessage);
      }
      else
      {
          fShoudIRun = shouldThreadsRun();
          condVarTraceWriter.wait();
      }
    }
 }

//This is the consumer. This is in my thread with lower priority
std::string CTraceFileWriterThread::fetchNext() 
{
    // When there are a lot of logs, I mean A LOT, I believe the 
    // control stays in this function for a long time and an other 
    // thread calling the "add" function is not able to acquire the lock
    // since its held here.

    std::string message;

    if( !writeQueue.empty() )
    {
      writeQueueMutex.lock();        // Obj of our wrapper around pthread_mutex_lock 
      message = writeQueue.front();  
      writeQueue.pop();              // std::queue
      writeQueueMutex.unLock() ;
    }
    return message;
}




//  This is the producer and is called from multiple threads.
void CTraceFileWriterThread::add( std::string outputString ) {

if ( !outputString.empty() )
{
    // crashes here while trying to acquire the lock when there are lots of
    // logs in prod systems.

    writeQueueMutex.lock();
    const size_t writeQueueSize = writeQueue.size();

    if ( writeQueueSize == maximumWriteQueueCapacity )
    {
        outputString.append ("\n queue full, discarding traces, traces are incomplete" );
    }

    if ( writeQueueSize <= maximumWriteQueueCapacity )
    {
        bool wasEmpty = writeQueue.empty();
        writeQueue.push(outputString);



        condVarTraceWriter.post(); // will be waiting in a function which calls "fetchNext"

    }
    writeQueueMutex.unLock();

}


int wrapperMutex::lock() {
//#[ operation lock()

 int iRetval;
 int iRetry = 10;

 do
 {
    //
    iRetry--;
    tRfcErrno = pthread_mutex_lock (&tMutex);
    if ( (tRfcErrno == EINTR) || (tRfcErrno == EAGAIN) )
    {
        iRetval = RFC_ERROR;    
        (void)sched_yield();
    }
    else if (tRfcErrno != EOK)
    {
        iRetval = RFC_ERROR;    
        iRetry = 0;
    }
    else
    {
        iRetval = RFC_OK;    
        iRetry = 0;
    }
 } while (iRetry > 0);

 return iRetval;


//#]

}

我生成了核心转储,并使用GDB对其进行了分析,这是一些发现

  1. 程序终止于信号11,分段错误。

  2. 我试图获取锁的添加功能处的“ Errno = 45”。我们在pthread_mutex_lock周围拥有的包装器试图在放弃之前获得大约10次锁。

当日志较少时,代码可以正常工作。另外,我们没有C ++ 11或更高版本,因此仅限于QNX互斥。感谢您的帮助,因为我在这个问题上研究了一个多月,进展甚微。请询问是否需要更多信息。

0 个答案:

没有答案