Ace_messae_block combines ace_allocator to make ace_message_block more flexible. This article describes how to use ace_message_block and ace_allocator and the precautions.
First, let's look at the Construction Function of ace_message_block: Ace_message_block (size_t size,
Ace_message_type type = Mb_data,
Ace_message_block * Cont = 0 ,
Const Char * Data = 0 ,
Ace_allocator * Allocator_strategy = 0 ,
Ace_lock * Locking_strategy = 0 ,
Unsigned Long Priority = Ace_default_message_block_priority,
Const Ace_time_value & Execution_time = Ace_time_value: zero,
Const Ace_time_value & Deadline_time = Ace_time_value: max_time,
Ace_allocator * Data_block_allocator = 0 ,
Ace_allocator * Message_block_allocator = 0 );
Three aliclouders are used: allocator_stratey, data_block_allocator, and message_block_allocator. The three aliclouders allocate memory for two objects in ace_message_block and the devices themselves. data_block, and the char * Memory pointed to by data_block.
In this way, we can implement the Effect of Pre-allocating memory by inputting three sub-shards.
Msg_allocator _ = New Ace_cached_allocator < Ace_message_block, ace_synch_mutex > (Msgcount );
Data_allocator _ = New Ace_cached_allocator < Ace_data_block, ace_synch_mutex > (Buffcount );
Buff_allocator _ = New Ace_cached_allocator < Mem_buf, ace_synch_mutex > (Buffcount );
ViewCodeYou can find that when calling its release method, it will first check whether it is allocated. If it is not used, delete it directly. If a distributor is used, the memory will be returned to the distributor.
You must also pay attention to a parameter, locking_strategy, which is a multi-threaded synchronization lock. It is mainly used for thread security control during the shallow copy or release process. For example, you need to increase or decrease the reference counter when calling duplicate or release.
How to reasonably use locking_strategy in the next writing so that it does not affect efficiency ,:)