1# Niobe407开发板OpenHarmony内核编程开发——mutex 2本示例将演示如何在Niobe407开发板上使用cmsis 2.0 接口进行互斥锁开发 3 4## mutex API分析 5 6## osThreadNew() 7 8```c 9osThreadId_t osThreadNew(osThreadFunc_t func, void *argument,const osThreadAttr_t *attr ) 10``` 11**描述:** 12 13函数osThreadNew通过将线程添加到活动线程列表并将其设置为就绪状态来启动线程函数。线程函数的参数使用参数指针*argument传递。当创建的thread函数的优先级高于当前运行的线程时,创建的thread函数立即启动并成为新的运行线程。线程属性是用参数指针attr定义的。属性包括线程优先级、堆栈大小或内存分配的设置。可以在RTOS启动(调用 osKernelStart)之前安全地调用该函数,但不能在内核初始化 (调用 osKernelInitialize)之前调用该函数。 14> **注意** :不能在中断服务调用该函数 15 16 17**参数:** 18 19|名字|描述| 20|:--|:------| 21| func | 线程函数. | 22| argument |作为启动参数传递给线程函数的指针| 23| attr |线程属性| 24 25## osMutexNew() 26 27```c 28osMutexNew (const osMutexAttr_t *attr); 29``` 30**描述:** 31 32创建互斥锁 33 34 35**参数:** 36 37|名字|描述| 38|:--|:------| 39| attr |mutex属性| 40 41## osMutexAcquire() 42 43```c 44osMutexAcquire (osMutexId_t mutex_id, uint32_t timeout); 45``` 46**描述:** 47 48获取互斥锁 49 50 51**参数:** 52 53|名字|描述| 54|:--|:------| 55| mutex_id |mutex ID| 56| timeout |delay时间| 57 58 59## osMutexRelease() 60 61```c 62osMutexRelease (osMutexId_t mutex_id); 63``` 64**描述:** 65 66释放互斥锁 67 68 69**参数:** 70 71|名字|描述| 72|:--|:------| 73| mutex_id |mutex ID| 74 75 76## 软件设计 77 78**主要代码分析** 79 80在os_mutex_example函数中,通过osThreadNew()函数创建了firstThread、twoThread、threeThread三个线程,firstThread、twoThread、threeThread三个线程启动后会输出打印日志。 81 82```c 83void firstThread(void) 84{ 85 osDelay(100U); 86 while(1) 87 { 88 osMutexAcquire(mutex_id, osWaitForever); 89 printf("firstThread is Acquire.\r\n"); 90 osDelay(100U); 91 osMutexRelease(mutex_id); 92 } 93} 94 95void twoThread(void) 96{ 97 osDelay(100U); 98 while(1) 99 { 100 printf("twoThread is Acquire.\r\n"); 101 osDelay(100U); 102 } 103} 104 105void threeThread(void) 106{ 107 while(1) 108 { 109 osMutexAcquire(mutex_id, osWaitForever); 110 printf("threeThread is Acquire.\r\n"); 111 osDelay(300U); 112 osMutexRelease(mutex_id); 113 } 114} 115 116void os_mutex_example(void) 117{ 118 osThreadAttr_t attr; 119 120 attr.attr_bits = 0U; 121 attr.cb_mem = NULL; 122 attr.cb_size = 0U; 123 attr.stack_mem = NULL; 124 attr.stack_size = 1024 * 4; 125 126 attr.name = "firstThread"; 127 attr.priority = 26; 128 if (osThreadNew((osThreadFunc_t)firstThread, NULL, &attr) == NULL) 129 { 130 printf("create firstThread failed!\n"); 131 } 132 133 attr.name = "twoThread"; 134 attr.priority = 25; 135 if (osThreadNew((osThreadFunc_t)twoThread, NULL, &attr) == NULL) 136 { 137 printf("create twoThread failed!\n"); 138 } 139 140 attr.name = "threeThread"; 141 attr.priority = 24; 142 if (osThreadNew((osThreadFunc_t)threeThread, NULL, &attr) == NULL) 143 { 144 printf("create threeThread failed!\n"); 145 } 146 147 mutex_id = osMutexNew(NULL); 148 if (mutex_id == NULL) 149 { 150 printf("create Mutex failed!\n"); 151 } 152} 153 154``` 155 156## 编译调试 157- 进入//kernel/liteos_m目录, 在menuconfig配置中进入如下选项: 158 159 `(Top) → Platform → Board Selection → select board niobe407 → use talkweb niobe407 application → niobe407 application choose` 160 161- 选择 `005_system_mutex` 162 163- 回到sdk根目录,执行`hb build -f`脚本进行编译。 164 165### 运行结果 166 167示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志 168``` 169threeThread is Acquire. 170twoThread is Acquire. 171twoThread is Acquire. 172firstThread is Acquire. 173twoThread is Acquire. 174threeThread is Acquire. 175twoThread is Acquire. 176twoThread is Acquire. 177twoThread is Acquire. 178firstThread is Acquire. 179twoThread is Acquire. 180threeThread is Acquire. 181twoThread is Acquire. 182``` 183