一. 摘要
ACE(自适配通信环境:Adaptive Communication Environment),封装了OS层的底层API,并结合设计模式为各种操作平台提供了一整套高性能的底层开发包。开发包的内容涉及到:进程间通信、内存管理、线程管理、同步、网络通信、事件分发、服务器配置等。
二. 适合阅读者
本文档适用ACE初学者,主要介绍如何利用ACE封装的优势来开发我们自己应用系统。
如果使用ACE类库提供的服务时例如:反应器(Reactor),需要在应用系统的开始和结束处各添加ACE::init()和ACE::fini(),分别用来初始化ACE环境和释放资源,另外对于单实例模式的使用也需要使用ACE::init和ACE::fini。这两个静态函数其实是调用ACE_Object_Manager类的init和fini方法。
对于ACE提供的单实例模式类,为了统一封装并不要求用户自己释放,而是由ACE在通过ACE::fini统一在系统的结束处释放其资源。
简单的线程类ACE_Thread
ACE_Thread实现了跨平台的线程封装,启动线程函数为静态函数ACE_Thread::spawn_n,通过join函数保证线程释放资源,按照OOP封装的原则并不建议用户过多的使用ACE_Thread,而应该采用后面介绍的ACE_Task,下面为简单的演示代码: #include "ace/os.h"
#include "ace/log_msg.h" #include "ace/thread.h" #include "ace/guard_t.h"
#include "ace/thread_mutex.h"
static void Worker(void * arg) {
ACE_DEBUG((LM_DEBUG, "线程测试\n")); }
int main(int argc, char ** argv) { ACE_thread_t idThread; ACE_hthread_t hThread; if (-1 == ACE_Thread::spawn_n (&idThread, 1,
(ACE_THR_FUNC)Worker, 0,
THR_JOINABLE | THR_NEW_LWP, ACE_DEFAULT_THREAD_PRIORITY, 0, 0,
&hThread)) {
ACE_DEBUG((LM_DEBUG, "starting threads failed\n")); return -1; }
getchar();
ACE_Thread::join(hThread); return 0; }
stdafx.h 的作用:
$Id: README 80826 2008-03-04 14:51:23Z wotte $
How to use the NT_Service example.
The NT_Service program has the following usage: -in -r -s -k -tn -d
-i: Install this program as an NT service, with specified startup
-r: Remove this program from the Service Manager
-s: Start the service
-k: Kill the service
-t: Set startup for an existing service
-d: Debug; run as a regular application
In order to see different stages of an NT service application,
you have to run the program several times, with different options.
Please note: run with only one option at a time.
1. Make executable NT_Service.
2. First, you must initialize the service in the NT SCM database.
Run NT_Service with -in, where n is one of the following startup options:
//
// Start Type (from WinNT.h)
//
#define SERVICE_SYSTEM_START 0x00000001
#define SERVICE_AUTO_START 0x00000002
#define SERVICE_DEMAND_START 0x00000003
#define SERVICE_DISABLED 0x00000004
If only -i is specified, SERVICE_AUTO_START is default option.
3. Now you are ready to run the actual service. Run NT_Service again,
this time with -s option. If the service starts successfully, it will
ring the system bell every second or so until the service is stopped.
4. To stop service execution, run NT_Service with the -k option.
5. To remove the service from the Service Control Manager database, run
NT_Service with -r.
In addition, once you have initialized this service (by using the -i option)
you can change its startup type to one of the other values above. To do
this, run NT_Service with -tn option. n is as explained above for -i.
In order to debug the service's execution itself, use the -d option.
支持unicode的ACE编译。
编译前在config.h中多添加如下两行,并且将项目默认字符集改为"使用 Unicode 字符集"。
#define ACE_USE_WCHAR
#define ACE_HAS_WCHAR
注意:
在windows平台下,如果没有定义如上两个宏, ACE会自动将 main 函数重定义, 并主动替你把 ACE::init() 和 ACE::fini() 调用好。添加这两个宏后,需要在main函数中手动调用。
main 函数重定义这一行为不能保证在所有平台和环境下都正常运作(如使用MFC时),其实不管如何,不妨在 main 函数的开头和结尾分别再调用一次 ACE::init() 和 ACE::fini()。 ACE::init() 和 ACE::fini() 里面都有嵌套层次计数机制,因此只要它们能够保证成对出现, 并且不出现交叉嵌套, 多调用一遍并不会出现任何问题