The memory pool is a static memory management method. It divides a fixed continuous memory area into several blocks of different sizes in advance. When applying, the user gives the corresponding memory block to him. The advantage of this method is that there is no memory fragmentation, but it is not flexible enough. It is suitable for scenarios requiring frequent access, such as buffer.
In this example, there are two threads. Thread1 continuously allocates memory blocks, but it does not use delay () to suspend itself. Therefore, thread2 cannot run because it has a lower priority than thread1. After all the memory blocks are allocated to thread1, try to obtain another memory block. Because the memory block has been allocated, thread1 will be suspended because it cannot obtain resources. At this time, thread2 starts to run and releases the memory block. After one is released, thread1 executes once when the waiting condition is met, and then thread2 continues to run until it ends.
Program:
# Include <rtthread. h> static rt_uint8_t * PTR [48]; static rt_uint8_t mempool [2, 4096]; static struct rt_mempool MP; static rt_thread_t tid1 = rt_null; static rt_thread_t tid2 = rt_null; static void thread1_entry (void * parameter) {int I, j = 1; char * block; while (j --) {for (I = 0; I <48; I ++) {/* apply for memory block */rt_kprintf ("allocate no. % d \ n ", I); If (PTR [I] = rt_null) {PTR [I] = rt_mp_alloc (& MP, rt_waiting_foreve R) ;}}/* continue to apply for a memory block because no memory block exists. The thread should be suspended */block = rt_mp_alloc (& MP, rt_waiting_forever ); rt_kprintf ("allocate the block mem \ n");/* release this memory block */rt_mp_free (Block); block = rt_null;} static void thread2_entry (void * parameter) {int I, j = 1; while (j --) {rt_kprintf ("try to release block \ n"); for (I = 0; I <48; I ++) {/* release all allocated memory blocks */If (PTR [I]! = Rt_null) {rt_kprintf ("release block % d \ n", I); rt_mp_free (PTR [I]); PTR [I] = rt_null ;}} /* sleep 10 OS tick */rt_thread_delay (10) ;}} int rt_application_init () {int I; for (I = 0; I <48; I ++) PTR [I] = rt_null;/* initialize the memory pool object. Each part is allocated with a size of 80, but there is also a control header with a size of 4, so the actual size is 84 */rt_mp_init (& MP, "MP1", & mempool [0], sizeof (mempool), 80 ); /* Create a thread 1 */tid1 = rt_thread_create ("T1", thread1_entry, rt_null, 512, 8, 10); I F (tid1! = Rt_null) rt_thread_startup (tid1);/* Create thread 2 */tid2 = rt_thread_create ("T2", thread2_entry, rt_null, 512, 9, 10); If (tid2! = Rt_null) rt_thread_startup (tid2); Return 0 ;}
Result:
allocate No.0allocate No.1allocate No.2allocate No.3allocate No.4allocate No.5allocate No.6allocate No.7allocate No.8allocate No.9allocate No.10allocate No.11allocate No.12allocate No.13allocate No.14allocate No.15allocate No.16allocate No.17allocate No.18allocate No.19allocate No.20allocate No.21allocate No.22allocate No.23allocate No.24allocate No.25allocate No.26allocate No.27allocate No.28allocate No.29allocate No.30allocate No.31allocate No.32allocate No.33allocate No.34allocate No.35allocate No.36allocate No.37allocate No.38allocate No.39allocate No.40allocate No.41allocate No.42allocate No.43allocate No.44allocate No.45allocate No.46allocate No.47try to release blockrelease block 0allocate the block memrelease block 1release block 2release block 3release block 4release block 5release block 6release block 7release block 8release block 9release block 10release block 11release block 12release block 13release block 14release block 15release block 16release block 17release block 18release block 19release block 20release block 21release block 22release block 23release block 24release block 25release block 26release block 27release block 28release block 29release block 30release block 31release block 32release block 33release block 34release block 35release block 36release block 37release block 38release block 39release block 40release block 41release block 42release block 43release block 44release block 45release block 46release block 47