Niobe WiFi IoT开发板OpenHarmony内核编程开发——mutex

本示例将演示如何在Niobe WiFi IoT开发板上使用cmsis 2.0 接口进行互斥锁开发

mutex API分析

osThreadNew()

c 复制代码
osThreadId_t osThreadNew(osThreadFunc_t	func, void *argument,const osThreadAttr_t *attr )

描述:

函数osThreadNew通过将线程添加到活动线程列表并将其设置为就绪状态来启动线程函数。线程函数的参数使用参数指针*argument传递。当创建的thread函数的优先级高于当前运行的线程时,创建的thread函数立即启动并成为新的运行线程。线程属性是用参数指针attr定义的。属性包括线程优先级、堆栈大小或内存分配的设置。可以在RTOS启动(调用 osKernelStart)之前安全地调用该函数,但不能在内核初始化 (调用 osKernelInitialize)之前调用该函数。

注意 :不能在中断服务调用该函数

参数:

名字 描述
func 线程函数.
argument 作为启动参数传递给线程函数的指针
attr 线程属性

osMutexNew()

c 复制代码
osMutexNew (const osMutexAttr_t *attr);

描述:

创建互斥锁

参数:

名字 描述
attr mutex属性

osMutexAcquire()

c 复制代码
osMutexAcquire (osMutexId_t mutex_id, uint32_t timeout);

描述:

获取互斥锁

参数:

名字 描述
mutex_id mutex ID
timeout delay时间

osMutexRelease()

c 复制代码
osMutexRelease (osMutexId_t mutex_id);

描述:

释放互斥锁

参数:

名字 描述
mutex_id mutex ID

软件设计

主要代码分析

在os_mutex_example函数中,通过osThreadNew()函数创建了firstThread、twoThread、threeThread三个线程,firstThread、twoThread、threeThread三个线程启动后会输出打印日志。

c 复制代码
void firstThread(void)
{
	osDelay(100U);
	while(1)
	{
		osMutexAcquire(mutex_id, osWaitForever);
    	printf("firstThread is Acquire.\r\n");
    	osDelay(100U);
		osMutexRelease(mutex_id);
	}
}

void twoThread(void)
{
	osDelay(100U);
	while(1) 
	{
		printf("twoThread is Acquire.\r\n");
    	osDelay(100U);
	}
}

void threeThread(void)
{
    while(1)
	{
		osMutexAcquire(mutex_id, osWaitForever);
		printf("threeThread is Acquire.\r\n");
		osDelay(300U);
		osMutexRelease(mutex_id);
	}
}

void os_mutex_example(void)
{
    osThreadAttr_t attr;

    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024 * 4;

    attr.name = "firstThread";
    attr.priority = 26;
    if (osThreadNew((osThreadFunc_t)firstThread, NULL, &attr) == NULL)
	{
    	printf("create firstThread failed!\n");
    }

	attr.name = "twoThread";
    attr.priority = 25;
    if (osThreadNew((osThreadFunc_t)twoThread, NULL, &attr) == NULL)
    {
      	printf("create twoThread failed!\n");
    }

    attr.name = "threeThread";
    attr.priority = 24;
    if (osThreadNew((osThreadFunc_t)threeThread, NULL, &attr) == NULL)
    {
      	printf("create threeThread failed!\n");
    }

	mutex_id = osMutexNew(NULL);
    if (mutex_id == NULL)
    {
      	printf("create Mutex failed!\n");
    }
}

编译调试

修改 BUILD.gn 文件

修改 applications/app路径下 BUILD.gn 文件,指定 mutex_example 参与编译。

r 复制代码
#"TW001_OS_helloworld:helloworld_example",
"TW005_OS_mutex:mutex_example",	
#"TW003_OS_timer:os_timer_example",
#"TW004_OS_event:os_event_example",
#"TW005_OS_mutex:os_mutex_example",
#"TW006_OS_semp:os_semaphore_example",
#"TW007_OS_message:os_message_example",

运行结果

示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志

c 复制代码
firstThread is Acquire
twoThread is Acquire
threeThread is Acquire

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:

如何快速入门:https://qr21.cn/FV7h05

  1. 基本概念
  2. 构建第一个ArkTS应用
  3. ......

开发基础知识:https://qr21.cn/FV7h05

  1. 应用基础知识
  2. 配置文件
  3. 应用数据管理
  4. 应用安全管理
  5. 应用隐私保护
  6. 三方应用调用管控机制
  7. 资源分类与访问
  8. 学习ArkTS语言
  9. ......

基于ArkTS 开发:https://qr21.cn/FV7h05

  1. Ability开发
  2. UI开发
  3. 公共事件与通知
  4. 窗口管理
  5. 媒体
  6. 安全
  7. 网络与链接
  8. 电话服务
  9. 数据管理
  10. 后台任务(Background Task)管理
  11. 设备管理
  12. 设备使用信息统计
  13. DFX
  14. 国际化开发
  15. 折叠屏系列
  16. ......

鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH

鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH

1.项目开发必备面试题

2.性能优化方向

3.架构方向

4.鸿蒙开发系统底层方向

5.鸿蒙音视频开发方向

6.鸿蒙车载开发方向

7.鸿蒙南向开发方向

相关推荐
一律清风1 小时前
【Opencv】canny边缘检测提取中心坐标
c++·opencv
belldeep3 小时前
如何阅读、学习 Tcc (Tiny C Compiler) 源代码?如何解析 Tcc 源代码?
c语言·开发语言
厦门辰迈智慧科技有限公司4 小时前
城市排水管网流量监测系统解决方案
运维·服务器
a东方青5 小时前
蓝桥杯 2024 C++国 B最小字符串
c++·职场和发展·蓝桥杯
周胡杰6 小时前
组件导航 (HMRouter)+flutter项目搭建-混合开发+分栏效果
前端·flutter·华为·harmonyos·鸿蒙·鸿蒙系统
国际云,接待6 小时前
云服务器的运用自如
服务器·架构·云计算·腾讯云·量子计算
bestadc6 小时前
鸿蒙 Core File Kit(文件基础服务)之简单使用文件
harmonyos
XiaoyaoCarter6 小时前
每日一道leetcode
c++·算法·leetcode·职场和发展·二分查找·深度优先·前缀树
galaxy_strive6 小时前
qtc++ qdebug日志生成
开发语言·c++·qt
Darkwanderor7 小时前
c++STL-list的模拟实现
c++·list