std :: lock_guard(mutex)产生死锁

时间:2018-05-22 21:59:43

标签: mutex deadlock

首先:感谢你阅读这个问题并试着去试试。帮助我我是整个线程主题的新手,我现在正面临一个严重的互斥锁死错误。

简短介绍:

几个月前我写了一个游戏引擎,它运行得很好,并且已经在游戏中使用了。该引擎基于SDL2。我希望通过使其线程安全来改进我的代码,这对于提高性能或使用其他一些理论概念非常有用。

问题:

游戏使用内部游戏阶段来显示游戏的不同状态,例如显示菜单或显示游戏的其他部分。当进入"小行星游戏" -stage我收到一个异常,这是由std :: lock_guard构造函数调用引发的。

详细问题:

当进入"小行星游戏" -stage时,正在调用modelGetDirection()函数来接收模型的方向向量。此函数使用lock_guard使此函数成为线程安全的。调试此代码部分时,这是抛出异常的地方。程序将进入此lock_guard构造函数并抛出异常。奇怪的是,这个函数以前从未被调用过。这是第一次调用此函数,每次测试都会在这里崩溃!

这是调试器在threadx中停止的地方:

inline int _Mtx_lockX(_Mtx_t _Mtx)
{   // throw exception on failure
return (_Check_C_return(_Mtx_lock(_Mtx)));
}

以下是我认为重要的实际代码片段:

互斥结构:

struct LEMutexModel
{
  // of course there are more mutexes inside here
  mutex modelGetDirection;
};

引擎类:

typedef class LEMoon
{
  private:

    LEMutexModel mtxModel;

    // other mutexes, attributes, methods and so on

  public:

    glm::vec2 modelGetDirection(uint32_t, uint32_t);

    // other methods
} *LEMoonInstance;

modelGetDirection()(引擎)函数定义:

glm::vec2 LEMoon::modelGetDirection(uint32_t id, uint32_t idDirection)
{
  lock_guard<mutex> lockA(this->mtxModel.modelGetDirection);
  glm::vec2 direction = {0.0f, 0.0f};
  LEModel * pElem = this->modelGet(id);

  if(pElem == nullptr)
    {pElem = this->modelGetFromBuffer(id);}

  if(pElem != nullptr)
    {direction = pElem->pModel->mdlGetDirection(idDirection);}
  else
  {
    #ifdef LE_DEBUG
      char * pErrorString = new char[256 + 1];
      sprintf(pErrorString, "LEMoon::modelGetDirection(%u)\n\n", id);
      this->printErrorDialog(LE_MDL_NOEXIST, pErrorString);
      delete [] pErrorString;
    #endif
  }

  return direction;
}

这是使用modelGetDirection方法的游戏功能!该功能将控制太空飞船:

void Game::level1ControlShip(void * pointer, bool controlAble)
{
  Parameter param = (Parameter) pointer;
  static glm::vec2 currentSpeedLeft = {0.0f, 0.0f};
  glm::vec2 speedLeft = param->engine->modelGetDirection(MODEL_VERA, LEFT);
  static const double INCREASE_SPEED_LEFT = (1.0f / VERA_INCREASE_LEFT) * speedLeft.x * (-1.0f);
// ... more code, I think that's not important
}

如前所述:当进入level1ControlShip()函数时,程序将进入modelGetDirection()函数。当进入modelGetDirection()函数时,当tryin&#39;致电:

lock_guard<mutex> lockA(this->mtxModel.modelGetDirection);

如前所述,这是整个应用程序运行中第一次调用此函数!

那为什么呢?我感谢任何帮助!整个引擎(不是游戏)是一个开源项目,可以在gitHub上找到,以防我忘记了一些重要的代码片段(抱歉!在这种情况下):

GitHub: Lynar Moon Engine

感谢您的帮助!

问候, 帕特里克

0 个答案:

没有答案
相关问题