在Windows dll中使用boost :: asio :: deadline_timer时出现死锁 [英] Deadlock while using boost::asio::deadline_timer in Windows dll

查看:257
本文介绍了在Windows dll中使用boost :: asio :: deadline_timer时出现死锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在DLL中使用Boost-Deadlinetimer,该DLL是使用boost :: dll :: shared_library加载的.以下代码段已精简为基本内容.

I am trying to use Boost-Deadlinetimer inside a DLL, which is loaded using boost::dll::shared_library. Following code-snippets are reduced to the essentials.

Example.h:

Example.h:

#include <boost/asio.hpp>
class Example  
{  
public:  
    Class() : m_timer(m_ioService) { } 
    virtual ~Class() { }  
    //...    
private:  
    boost::asio::io_service m_ioService;  
    boost::asio::deadline_timer m_timer;
    //...
};

Example.cpp:

Example.cpp:

#include "Example.h"
#include <boost/config.hpp>
//...
extern "C" BOOST_SYMBOL_EXPORT Example MyExample;
Example MyExample;

Main.cpp:

#include <boost/dll/Import.hpp>
//...
boost::dll::shared_library lib("Example.dll", boost::dll::load_mode::Default_mode);
//...

我的问题是,只要m_timer在构造函数的初始化列表中,加载编译的dll时就会出现死锁.

My Problem is that there is a deadlock while loading the compiled dll as long as m_timer is in the constructor's initialization list.

boost::shared_ptr替换m_timer并在构造函数(或后续函数)中对其进行初始化时,在加载 dll时不会出现 no 死锁,但是卸载 dll时出现死锁.

When replacing m_timer by a boost::shared_ptr and initializing that in the constructor (or a subsequent function), then there is no deadlock while loading the dll but a deadlock while unloading the dll.

无论如何,我不能真正在Windows dll中使用全局截止日期计时器对象.

Anyway, I cannot really use a global deadline timer object in a Windows dll.

推荐答案

Windows的

Windows has a LoaderLock which is held from the call to LoadLibrary until it returns.

Windows系统使用此锁来确保进程保持稳定,因为对级联的DLL进行了正确的引用计数.

This lock is used by the Windows system to ensure that the process remains stable, as cascaded DLLs are properly reference counted.

在DLL中创建的全局变量,由dynamic initialization在DllMain运行之前(DLL_PROCESS_ATTACH)构造,并在DllMain完成之后(DLL_PROCESS_DETACH)销毁.

Globals which are created within a DLL, are constructed by dynamic initialization just before DllMain runs (DLL_PROCESS_ATTACH), and destroyed just after DllMain finishes (DLL_PROCESS_DETACH).

使用全局变量创建DLL时,需要遵循链接中的规则,并避免...

When you create a DLL with a global variable, you need to follow the rules in the link, and avoid...

  • 加载DLL
  • 使用锁-导致锁反转的原因
  • 创建流程
  • 阅读注册表
  • 创建/销毁线程
  • 使用StringType函数.

最简单的方法是在LoadLibrary之后调用一个单独的初始化函数,并在最终初始化全局变量的FreeLibrary之前调用一个Uninitialize函数.

The easiest way to cope with this would be having a separate initialize function which is called after LoadLibrary, and a Uninitialize function called before the final FreeLibrary where global variables are separately initialized.

这篇关于在Windows dll中使用boost :: asio :: deadline_timer时出现死锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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