作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个完美的工作程序,它连接到摄像机(IDS uEye 摄像机)并不断从中抓取帧并显示它们。
但是,在连接到相机之前加载特定的 dll 时,程序会以 100% 的 CPU 负载运行。如果我在连接到相机后加载 dll,程序运行良好。
int main()
{
INT nRet = IS_NO_SUCCESS;
// init camera (open next available camera)
m_hCam = (HIDS)0;
// (A) Uncomment this for 100% CPU load:
// HMODULE handle = LoadLibrary(L"myInnocentDll.dll");
// This is the call to the 3rdparty camera vendor's library:
nRet = is_InitCamera(&m_hCam, 0);
// (B) Uncomment this instead of (A) and the CPU load won't change
// HMODULE handle = LoadLibrary(L"myInnocentDll.dll");
if (nRet == IS_SUCCESS)
{
/*
* Please note: I have removed all lines which are not necessary for the exploit.
* Therefore this is NOT a full example of how to properly initialize an IDS camera!
*/
is_GetSensorInfo(m_hCam, &m_sInfo);
GetMaxImageSize(m_hCam, &m_s32ImageWidth, &m_s32ImageHeight);
m_nColorMode = IS_CM_BGR8_PACKED;// IS_CM_BGRA8_PACKED;
m_nBitsPerPixel = 24; // 32;
nRet |= is_SetColorMode(m_hCam, m_nColorMode);
// allocate image memory.
if (is_AllocImageMem(m_hCam, m_s32ImageWidth, m_s32ImageHeight, m_nBitsPerPixel, &m_pcImageMemory, &m_lMemoryId) != IS_SUCCESS)
{
return 1;
}
else
{
is_SetImageMem(m_hCam, m_pcImageMemory, m_lMemoryId);
}
}
else
{
return 1;
}
std::thread([&]() {
while (true) {
is_FreezeVideo(m_hCam, IS_WAIT);
/*
* Usually, the image memory would now be grabbed via is_GetImageMem().
* but as it is not needed for the exploit, I removed it as well
*/
}
}).detach();
cv::waitKey(0);
}
独立于实际使用的相机驱动程序,加载 dll 以什么方式可以改变它的性能,占用所有可用 CPU 内核的 100%?使用 Visual Studio 诊断工具时,多余的 CPU 时间归因于“[External Call] SwitchToThread”,而不是 myInnocentDll。
最佳答案
经过大量挖掘,我找到了答案,它本身既简单又令人沮丧:
是微软对的支持不好OpenMP .当我在我的项目中禁用 OpenMP 时,相机驱动程序运行得很好。
原因似乎是微软编译器使用OpenMP,忙等待,也有手动配置的可能OMP_WAIT_POLICY
,但由于我并不依赖 OpenMP,因此禁用对我来说是最简单的解决方案。
关于c++ - 如何在我的主应用程序中加载一个 dll 会导致 100 个 CPU 负载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63136777/
我是一名优秀的程序员,十分优秀!