概述
1.优先级反转
#include <stdio.h>
#include <pthread.h>
#include <SylixOS.h>
static LW_HANDLE _G_Block;
static long long _G_Count = 0;
PVOID handler1 (PVOID pvarg)
{
int ret, i;
for (i = 0; i < 3000; ++i) {
printf("Low thread running.n");
}
ret = Lw_SemaphoreB_Wait(_G_Block, LW_OPTION_WAIT_INFINITE);
if (ret != ERROR_NONE) {
return (LW_NULL);
}
while (1) {
_G_Count++;
printf("handler1:count = %lldn", _G_Count);
if (_G_Count > 100000000) {
return (LW_NULL);
}
}
Lw_SemaphoreB_Post(_G_Block);
return (LW_NULL);
}
PVOID handler2 (PVOID pvarg)
{
int ret;
int i;
for (i = 0; i < 1000; ++i) {
printf("High thread running.n");
}
sleep(5);
ret = Lw_SemaphoreB_Wait(_G_Block, LW_OPTION_WAIT_INFINITE);
if (ret != ERROR_NONE) {
return (LW_NULL);
}
_G_Count++;
printf("handler2:count = %lldn", _G_Count);
Lw_SemaphoreB_Post(_G_Block);
return (LW_NULL);
}
PVOID handler3 (PVOID pvarg)
{
long long t = 0;
while (1) {
t++;
printf("handler3:t = %lldn", t);
}
return (LW_NULL);
}
int main (int argc, char **argv)
{
LW_HANDLE hThread1;
LW_HANDLE hThread2;
LW_HANDLE hThread3;
_G_Block = Lw_SemaphoreB_Create("count_lock",
LW_TRUE,
LW_OPTION_WAIT_PRIORITY |
LW_OPTION_OBJECT_LOCAL,
LW_NULL);
if (_G_Block == LW_OBJECT_HANDLE_INVALID) {
printf("fail to create semaphoreBn");
return (PX_ERROR);
}
hThread1 = Lw_Thread_Create("handler_1", handler1, LW_NULL, LW_NULL);
if (hThread1 == LW_OBJECT_HANDLE_INVALID) {
printf("fail to create handler_1");
return (PX_ERROR);
}
hThread2 = Lw_Thread_Create("handler_2", handler2, LW_NULL, LW_NULL);
if (hThread2 == LW_OBJECT_HANDLE_INVALID) {
printf("fail to create handler_2");
return (PX_ERROR);
}
sleep(10);
hThread3 = Lw_Thread_Create("handler_3", handler3, LW_NULL, LW_NULL);
if (hThread3 == LW_OBJECT_HANDLE_INVALID) {
printf("fail to create handler_3");
return (PX_ERROR);
}
Lw_Thread_SetPriority(hThread1, 220);
Lw_Thread_SetPriority(hThread2, 205);
Lw_Thread_SetPriority(hThread3, 210);
Lw_Thread_Join(hThread1, LW_NULL);
Lw_Thread_Join(hThread2, LW_NULL);
Lw_Thread_Join(hThread3, LW_NULL);
Lw_SemaphoreB_Delete(&_G_Block);
return (PX_ERROR);
}
2.解决方法之一
#include <stdio.h>
#include <pthread.h>
#include <SylixOS.h>
static LW_HANDLE _G_Block;
static long long _G_Count = 0;
PVOID handler1 (PVOID pvarg)
{
int ret, i;
for (i = 0; i < 300; ++i) {
printf("Low thread running.n");
}
ret = Lw_SemaphoreM_Wait(_G_Block, LW_OPTION_WAIT_INFINITE);
if (ret != ERROR_NONE) {
return (LW_NULL);
}
while (1) {
_G_Count++;
printf("Low: count = %lldn", _G_Count);
if (_G_Count > 100000000) {
return (LW_NULL);
}
}
Lw_SemaphoreM_Post(_G_Block);
return (LW_NULL);
}
PVOID handler2 (PVOID pvarg)
{
int ret;
int i;
for (i = 0; i < 1000; ++i) {
printf("High thread running.n");
}
sleep(2);
ret = Lw_SemaphoreM_Wait(_G_Block, LW_OPTION_WAIT_INFINITE);
if (ret != ERROR_NONE) {
return (LW_NULL);
}
while (1) {
_G_Count++;
printf("High: count = %lldn", _G_Count);
}
Lw_SemaphoreM_Post(_G_Block);
return (LW_NULL);
}
PVOID handler3 (PVOID pvarg)
{
long long t = 0;
while (1) {
t++;
printf("mid: t = %lldn", t);
}
return (LW_NULL);
}
int main (int argc, char **argv)
{
LW_HANDLE hThread1;
LW_HANDLE hThread2;
LW_HANDLE hThread3;
LW_CLASS_THREADATTR threadattr;
_G_Block = Lw_SemaphoreM_Create("count_lock",
LW_PRIO_HIGH,
LW_OPTION_WAIT_FIFO |
LW_OPTION_OBJECT_LOCAL |
LW_OPTION_INHERIT_PRIORITY,
LW_NULL);
if (_G_Block == LW_OBJECT_HANDLE_INVALID) {
printf("fail to create semaphoreBn");
return (PX_ERROR);
}
Lw_ThreadAttr_Build(&threadattr,
4 * LW_CFG_KB_SIZE,
LW_PRIO_NORMAL + 1,
LW_OPTION_THREAD_STK_CHK,
LW_NULL);
hThread1 = Lw_Thread_Create("t_low", handler1, &threadattr, LW_NULL);
if (hThread1 == LW_OBJECT_HANDLE_INVALID) {
printf("fail to create handler_1");
return (PX_ERROR);
}
Lw_ThreadAttr_Build(&threadattr,
4 * LW_CFG_KB_SIZE,
LW_PRIO_NORMAL - 1,
LW_OPTION_THREAD_STK_CHK,
LW_NULL);
hThread2 = Lw_Thread_Create("t_high", handler2, &threadattr, LW_NULL);
if (hThread2 == LW_OBJECT_HANDLE_INVALID) {
printf("fail to create handler_2");
return (PX_ERROR);
}
sleep(5);
Lw_ThreadAttr_Build(&threadattr,
4 * LW_CFG_KB_SIZE,
LW_PRIO_NORMAL,
LW_OPTION_THREAD_STK_CHK,
LW_NULL);
hThread3 = Lw_Thread_Create("t_mid", handler3, &threadattr, LW_NULL);
if (hThread3 == LW_OBJECT_HANDLE_INVALID) {
printf("fail to create handler_3");
return (PX_ERROR);
}
// Lw_Thread_SetPriority(hThread1, 220);
// Lw_Thread_SetPriority(hThread2, 205);
// Lw_Thread_SetPriority(hThread3, 210);
Lw_Thread_Join(hThread1, LW_NULL);
Lw_Thread_Join(hThread2, LW_NULL);
Lw_Thread_Join(hThread3, LW_NULL);
Lw_SemaphoreM_Delete(&_G_Block);
return (PX_ERROR);
}
最后
以上就是顺心西装为你收集整理的优先级反转以及解决方法(基于SylixOS)的全部内容,希望文章能够帮你解决优先级反转以及解决方法(基于SylixOS)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复