#include <osal.h>
#define USER_TASK1_PRI 12 //低优先级
#define USER_TASK2_PRI 11 //高优先级
uint32_t public_value = 0;
osal_mutex_t public_value_mutex;
//任务1:
static int user_task1_entry()
{
while (1)
{
if (true == osal_mutex_lock(public_value_mutex))
{
printf("\r\ntask1 : lock a mutex.\r\n");
public_value += 10;
printf("task1:public_value = %ld.\r\n", public_value);
printf("task1:unlock a mutex.\r\n\r\n");
if (true == osal_mutex_unlock(public_value_mutex))
{
printf("task1:unlock a mutex OK.\r\n\r\n");
osal_task_sleep(10);
}
else
printf("task1:Lock a mutex ERROR.\r\n");
if (public_value > 60)
osal_mutex_del(public_value_mutex);
if (public_value > 100)
break;
}
else
{
//打印一次跳出循环
printf("task1:Lock a mutex ERROR.\r\n");
break;
}
}
return 0;
}
//任务2:
static int user_task2_entry()
{
while (1)
{
if (true == osal_mutex_lock(public_value_mutex))
{
printf("\r\n task2 : lock a mutex.\r\n");
public_value += 5;
printf("task2:public_value = %ld.\r\n", public_value);
if (true == osal_mutex_unlock(public_value_mutex))
printf("task2:unlock a mutex OK.\r\n\r\n");
else
printf("task2:unlock a mutex ERROR.\r\n\r\n");
if (public_value > 90)
break;
osal_task_sleep(10);
}
else
{
printf("\r\ntask2:Lock a mutex ERROR.\r\n");
break;
}
}
return 0;
}
//创建互斥锁
int standard_app_demo_main()
{
/*创建互斥锁public_value_mutex*/
osal_mutex_create(&public_value_mutex);
/*创建任务task1*/
osal_task_create("user1_task", user_task1_entry, NULL, 0x400, NULL, USER_TASK1_PRI);
osal_task_create("user2_task", user_task2_entry, NULL, 0x400, NULL, USER_TASK2_PRI);
}
华为LiteOS互斥锁任务抢占-osal_semp_demo
最新推荐文章于 2023-05-08 17:45:36 发布