内存池
优质
小牛编辑
139浏览
2023-12-01
线程从内存池中获得内存块与释放内存块
线程从内存池中获得内存块与释放内存块
源码
/* * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2018-08-24 yangjie the first version *//* * 程序清单:内存池例程 * * 这个程序会创建一个静态的内存池对象,2个动态线程。 * 一个线程会试图从内存池中获得内存块,另一个线程释放内存块 * 内存块 */#include <rtthread.h>static rt_uint8_t *ptr[50];static rt_uint8_t mempool[4096];static struct rt_mempool mp;#define THREAD_PRIORITY 25#define THREAD_STACK_SIZE 512#define THREAD_TIMESLICE 5/* 指向线程控制块的指针 */static rt_thread_t tid1 = RT_NULL;static rt_thread_t tid2 = RT_NULL;/* 线程1入口 */static void thread1_mp_alloc(void *parameter){ int i; for (i = 0 ; i < 50 ; i++) { if (ptr[i] == RT_NULL) { /* 试图申请内存块50次,当申请不到内存块时, 线程1挂起,转至线程2运行 */ ptr[i] = rt_mp_alloc(&mp, RT_WAITING_FOREVER); if (ptr[i] != RT_NULL) rt_kprintf("allocate No.%d\n", i); } }}/* 线程2入口,线程2的优先级比线程1低,应该线程1先获得执行。*/static void thread2_mp_release(void *parameter){ int i; rt_kprintf("thread2 try to release block\n"); for (i = 0; i < 50 ; i++) { /* 释放所有分配成功的内存块 */ if (ptr[i] != RT_NULL) { rt_kprintf("release block %d\n", i); rt_mp_free(ptr[i]); ptr[i] = RT_NULL; } }}int mempool_sample(void){ int i; for (i = 0; i < 50; i ++) ptr[i] = RT_NULL; /* 初始化内存池对象 */ rt_mp_init(&mp, "mp1", &mempool[0], sizeof(mempool), 80); /* 创建线程1:申请内存池 */ tid1 = rt_thread_create("thread1", thread1_mp_alloc, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); if (tid1 != RT_NULL) rt_thread_startup(tid1); /* 创建线程2:释放内存池*/ tid2 = rt_thread_create("thread2", thread2_mp_release, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE); if (tid2 != RT_NULL) rt_thread_startup(tid2); return 0;}/* 导出到 msh 命令列表中 */MSH_CMD_EXPORT(mempool_sample, mempool sample);