的std :: unique_lock<的std ::互斥>禁止dll卸载 [英] std::unique_lock<std::mutex> prohibit dll unloading

查看:111
本文介绍了的std :: unique_lock<的std ::互斥>禁止dll卸载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

卸载一个DLL时我有一个问题。就像,但退出不同。我正在加载一个dll使用 LoadLibraryA 然后调用函数并关闭dll与 FreeLibrary 。但是,dll并不是卸载,而是FreeLibrary返回成功。
减少代码:

  void foo(){
std :: unique_lock< std :: mutex&锁(mtx_);
}

调试代码并查看 Process Explorer unique_lock创建第二个线程但为什么?只要应用程序运行,此线程也正在运行。没有别的没有其他处理dll,没有其他功能。此外,dll仍然加载在程序中。如果我删除上面的一行,一切都很好。 dll卸载的很好,没有额外的线程。所以我的问题是,如何避免这种行为,为什么unique_lock创建一个线程?



互斥体用于多线程,但是在测试,只有一个线程,加载dll调用foo,并卸载dll。



编辑:



我不知道这是mutex / unique_lock的visual studio实现中的错误,但是我使用boost的mutex / unique_lock解决了这个问题。

解决方案

这是Visual Studio中的一个错误,当您使用std :: thread或std :: mutex等时,它们异常增加了DLL的引用计数。



请参阅这个错误报告。


I have an issue while unloading a dll. It is like this one but quit different. I'm loading a dll using LoadLibraryA then call a function and close the dll with FreeLibrary. However, the dll is not unloaded but FreeLibrary return success. reduced code:

void foo() {
    std::unique_lock<std::mutex> lock(mtx_);
}

While debugging the code and looking into Process Explorer unique_lock creates a second thread, but why? Also this thread is running as long as the application runs. There is nothing else; no other handle to the dll, no other functions. Also the dll is still loaded in the program. If I remove the line above, everything is fine. The dll is unloaded fine, no extra threads. So my question is, how to avoid this behavior and why is unique_lock creating a thread?

The mutex is intended for multithreading, but while testing, there is only one thread, loading the dll calling foo, and unloading the dll.

edit:

I don't know if this is a bug in the visual studio implementation of mutex/unique_lock but I solved the problem by using boost's mutex/unique_lock.

解决方案

This is a bug in Visual Studio, they increase the reference count of the DLL abnormally when you use std::thread or std::mutex, etc.

See this bug report.

这篇关于的std :: unique_lock&LT;的std ::互斥&GT;禁止dll卸载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆