这些都是因为我们使用了Aggressive Flush(又称为 Flush-On-Close)的方式来保存注册表,它在每次调用RegCloseKey之后都会调用一次RegFlushKey,这种方式对于使用永久存储器的设备来说是不可取的。那么是否有更好的办法呢?答案是肯定的。
(1) 首先,我们去掉Aggressive Flush,它的实现是在注册表里面以下位置设置RegistryFlags:
[HKEY_LOCAL_MACHINE/init/BootVars]
"SystemHive"="system.hv"
"DefaultUser"="default"
"Flags"=dword:3
"RegistryFlags"=dword:1
将RegistryFlags的值修改为0或者去掉RegistryFlags这个键值均可,详细含义可以到MS的帮助文档里面查找。
(2) 然后,使能Lazy Flush (Registry Flush Thread)
在编译脚本或者命令行中 设置环境变量 SET PRJ_ENABLE_REGFLUSH_THREAD=1,重新sysgen (blddemo -q),在release目录下的reginit.ini文件中我们可以看到注册表新增以下内容:
IF PRJ_ENABLE_REGFLUSH_THREAD
[HKEY_LOCAL_MACHINE/System/ObjectStore/RegFlush]
; To monitor the flushing from an external process add "ActivityName" registry value.
; The activity name is a global named event that filesystem will signal on Registry Activity.
; "ActivityName"=""
; Create an thread in filesys to perform flushing
"SpawnThread"=dword:1
; Make the thread IDLE priority
"FlushPriority256"=dword:FF
; ActivityThreshold specifies the # of reg activity before we force a flush
"ActivityThreshold"=dword:100
; Timeout period for a flush (flush occurs if there have been some changes during this period)
"FlushPeriod"=dword:3E8
ENDIF
系统会按照SpawnThread新建一个优先级为FlushPriority256的线程,按照FlushPeriod(默认值为0x3E8,1000ms)周期性地去检查注册表的变化并加以保存。