如何在Visual Studio中查看std :: mutex的锁定线程?

时间:2019-04-30 10:00:21

标签: c++ multithreading visual-studio

使用Windows CRITICAL_SECTION,我可以通过扩展变量来查看锁定它的线程:

enter image description here

但是,我似乎无法对std::mutex做同样的事情,而是得到了很多无用的值:

enter image description here

是否有不需要修改我代码的方法?

1 个答案:

答案 0 :(得分:1)

感谢@PeterT的评论,为各种互斥类型(位于/ Documents / Visual Studio 2017 / Visualizers / mutex.natvis中)编写了可视化器:

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
  <Type Name="std::_Mutex_base">
    <Expand>
      <Item Name="[thread_id]">*(long*)((char*)&amp;_Mtx_storage+sizeof(_Mtx_storage)-8)</Item>
      <Item Name="[count]">*(int*)((char*)&amp;_Mtx_storage+sizeof(_Mtx_storage)-4)</Item>
    </Expand>    
  </Type>
  <Type Name="std::mutex">
    <DisplayString>mutex</DisplayString>
    <Expand>
      <ExpandedItem>(_Mutex_base*)this</ExpandedItem>
    </Expand>
  </Type>
  <Type Name="std::timed_mutex">
    <DisplayString>timed_mutex</DisplayString>
    <Expand>
      <Item Name="[locked]">_My_locked</Item>
    </Expand>
  </Type>
  <Type Name="std::recursive_mutex">
    <DisplayString>recursive_mutex</DisplayString>
    <Expand>
      <ExpandedItem>(_Mutex_base*)this</ExpandedItem>
    </Expand>
  </Type>
  <Type Name="std::recursive_timed_mutex">
    <DisplayString>recursive_timed_mutex</DisplayString>
    <Expand>
      <Item Name="[locked]">_My_locked</Item>
      <Item Name="[owner]">_My_owner</Item>
    </Expand>
  </Type>
</AutoVisualizer>