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

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

问题描述

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

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_);
}

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

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?

互斥体是为多线程, ,只有一个线程,加载dll调用foo和卸载dll。

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

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

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.

推荐答案

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

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.

请参阅 this 错误报告。

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

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