找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 5842|回复: 3

关于ACE_Singleton。。。

[复制链接]
发表于 2009-6-17 14:58:36 | 显示全部楼层 |阅读模式
我在这个帖子里面看到说关于ace的单件,会在dll和exe中存在不同的实例。
http://www.acejoy.com/space/html/68/t-768.html
解决方法是使用ACE_Unmanaged_singleton.
看了ACE_Unmanaged_singleton是继承自ACE_singleton,
连ACE_singleton都会有不同实例了,那么ACE_Unmanaged_singleton呢?
看ace头文件的说明,说他俩区别只是释放问题。
我想知道哪本书上面有这个介绍,谢谢~~~~不好意思。。。
发表于 2009-6-17 15:03:52 | 显示全部楼层
ACE程序员指南、C++网络编程,卷1、卷2.
这是必读读物。
19.8 Singletons and Services
Recall from Section 1.6.3 that the ACE_Object_Manager provides a useful facility for ensuring that objects can be cleaned up properly when your program runs down. The ACE_Singleton class is integrated with the Object Manager facility, and many application services use this feature to manage singleton lifetimes correctly. However, when using this facility, you must remember when the singleton objects are destroyed: at program rundown, when the ACE_Object_Manager does its cleanup work. Now consider what happens when a dynamically loaded service that makes use of ACE_Singleton is unloaded before the Object Manager cleans up singletons. Usually, very bad things happen. The code that was to run the destruction of the service's singleton is probably not mapped into your process when the ACE_Object_Manager cleanup happens, and you'll likely get an access violation.

To make the instantiate-when-needed, double-checked locking safety of ACE_Singleton available to dynamically loaded services without the danger of having the cleanup performed after the service is unloaded, ACE offers the ACE_Unmanaged_Singleton class, which is used exactly like ACE_Singleton, except that an unmanaged singleton is not registered with ACE_Object_Manager and is not automatically cleaned up at program termination. To delete an unmanaged singleton, you must call its close() method. You should do this from your service's fini() method, as that's the dynamic service equivalent of program termination.
 楼主| 发表于 2009-6-17 20:09:46 | 显示全部楼层
谢谢~~~!
通过测试程序发现ACE_Singleton和ACE_Unmanaged_Singleton在dll中和exe中都是两个实例.
在dll中定义:
        __Export teststruct* getsingleton()
        {
                return ACE_Singleton<teststruct, ACE_Thread_Mutex>::instance();
        }
        __Export teststruct* getunamagedsingleton()
        {
                return ACE_Unmanaged_Singleton<teststruct, ACE_Thread_Mutex>::instance();
        }
        
然后再在测试程序中调用
        pdll = getsingleton();
        pexe = ACE_Singleton<teststruct, ACE_Thread_Mutex>::instance();
        ACE_DEBUG((LM_DEBUG, "Singleton: dll%@ exe%@\n", pdll, pexe));
        
        pdll = getunamagedsingleton();
        pexe = ACE_Unmanaged_Singleton<teststruct, ACE_Thread_Mutex>::instance();
        ACE_DEBUG((LM_DEBUG, "UnmanagedSingleton: dll%@ exe%@\n", pdll, pexe));
        
结果这4个指针的值都不相同.


然后,把结构体在dll头文件中这样一下就好了,就一个实例了。。。

#ifdef BUILD_XXX_DLL
    class __declspec(dllexport) teststructManager : public ACE_Singleton<teststruct, ACE_Thread_Mutex>
#else
    class __declspec(dllimport) teststructManager : public ACE_Singleton<teststruct, ACE_Thread_Mutex>
#endif
    {};
就好了。。。
要么只是export/import teststruct。。。?没试

[ 本帖最后由 sokiiya 于 2009-6-18 11:05 编辑 ]
发表于 2010-9-10 21:15:39 | 显示全部楼层
路过此地,学习学习
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-11-22 06:55 , Processed in 0.019779 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表