Objective:
The students who have done GUI development are aware of the dual caching mechanism . The process is to first draw all of the scene and entity objects to a backup canvas, and then fill the contents of the backup canvas into a real artboard canvas. If you do not adopt a dual-cache mechanism, your screen may flicker and jitter.
The reason is the entire drawing process, including the clear screen, drawing scenes and individual entities. It takes much more time than a single canvas to replicate. the rate at which the CPU writes canvas is less than the rate at which the LCD reads the canvas . So there is a flicker phenomenon.
In the background service, you will also encounter a similar situation: when the data/resource needs to be updated, the use of direct incremental update is expensive (time-consuming, blocking service available/real-time response), thus introducing back buffer, do 0/1 switch .
In this paper, "Configuration file Hot Load Update" as an example, focus on the 0/1 switching ideas and optimization techniques.
Hot Load Update:
It is often necessary to restart the service process in the past when the configuration is updated. In order to improve the availability of services, more convenient operation.
The ways to improve this are:
1) Introduction of the Configuration Center Service (configserver)
The configuration file of the module is placed in the configserver, and the specific module is obtained from the Configserver (pull/notify).
2) Monitor Local configuration file changes
The process module, through periodic polling/event triggering, senses whether the configuration file has changed and, if so, re-loaded.
However, in any way, there is bound to be a switching process.
Switch Features:
Both sides of the switch are defined as front-end and back-end, the front-end resources are often accessed by n threads (static read-only), and the backend resource is often a thread-updated write. So the formation of a N read 1 write pattern.
During the implementation of C + +, the switching process is often a pointer re-assignment, very simple.
But the problem is hidden in this, in the old resource destruction process after switching, there is a multi-threaded race conflict risk.
One might suggest that there is no problem if the access to resources and the switching of resources are protected by the same lock . However, in the low frequency switching scenario, the lock brings the performance loss, some outweigh the gains.
No lock 0/1 Toggle:
Is there a lock-free switching method?
1). delayed destruction
worker threads hold and access old resource handles for a long time, you can set a time window that is within the period of protection, Destruction of old resources is prohibited.
Note: In most scenarios, the scenario satisfies the criteria. In theory, the low probability event is not ruled out.
2). smart pointer toggle with reference count
We build a small example of switching with boost shared_ptr
#include <boost/shared_ptr.hpp> #include <stdint.h> #include < Stdio.h>class Config {};class DataCenter {public:datacenter () {} void init () {active_idx = 0; Switchover[active_idx].reset (New Config ()); }//*) toggle function, called by update thread void Swith () {uint32_t Unactive_idx = (Active_idx = = 0)? 1:0; uint32_t old_active_idx = Active_idx; *) New resources Ready Switchover[unactive_idx].reset (); *) formally switch active_idx = UNACTIVE_IDX; *) Old resource Reset, introduction count minus one switchover[old_active_idx].reset (); }//*) to access the resource, called by the front-end thread boost::shared_ptr<config> getconfig () {return SWITCHOVER[ACTIVE_IDX]; }private:volatile uint32_t Active_idx; *) Toggle array boost::shared_ptr<config> switchover[2];};
Skillfully using boost::shared_ptr inside there is an atomic counter and proxy pointers , with the help of Raii's idea of the perfect implementation of the non-citation of the automatic cleaning work . The above-mentioned competing conflicts are also avoided.
Summarize:
In the service module of the 0/1 switch there are many, here is a brief description of the next solution, no detailed deployment, right when the personal study notes.
Written at the end:
If you think this article is helpful to you, please give it a little reward. In fact, I would like to try to see if blogging can bring me a little bit of revenue. No matter how much, is a kind of sincere affirmation to the landlord.
Talking about 0/1 switch