正如 cmets 中所述,这是一项复杂的任务。
一种方法是创建一个像这样的类:
class UART_access_manager
{
typedef std::multimap<int, dispatch_semaphore_t> priority_map_t;
public:
UART_access_manager();
~UART_access_manager();
void access_manager(unsigned int prio, dispatch_semaphore_t pSem);
private:
void amInext(unsigned int prio);
void process_priority(unsigned int prio);
void priority_locker(unsigned int prio);
void priority_unlocker(unsigned int prio);
int count;
std::mutex data_mtx, priority_0_mtx, mtx;
priority_map_t priority_map;
};
请注意,我在 OSX 上构建了测试并且无法使用未命名的信号量,我没有检查调度是否在其他操作系统上可用(可能不是作为它的苹果库)。
UART_access_manager::UART_access_manager()
{
}
UART_access_manager::~UART_access_manager()
{
}
/*********************************************************************
*
* Function: void UART_access_manager::access_manager(unsigned
int prio, dispatch_semaphore_t pSem)
*
* Description: add an UART access request to the queue based on priority & start process based on type
*
* Notes: 0 is highest
*
* Returns: none
*
*********************************************************************/
void UART_access_manager::access_manager(unsigned int prio, dispatch_semaphore_t pSem)//, add parameters at will
{
int counter = 1; //debug only
while (counter) //should check for termination criteria
{
//check if something was pushed on the file descriptor
if( counter == 10) //add run condition
{
counter = 0; //debug
priority_locker(prio);
priority_map_t::iterator it = priority_map.insert(std::pair<int, dispatch_semaphore_t>(prio, pSem) );
printf("\n thread with priority %d added to queue(size %lu)", prio, priority_map.size());
amInext(prio);
priority_unlocker(prio);
while(dispatch_semaphore_wait(pSem, DISPATCH_TIME_NOW) != 0){};
priority_locker(prio);
// do the actual job
process_priority(prio);
// done, remove yourself
priority_map.erase(it);
if( ! priority_map.empty() )
{
// let next guy run:
dispatch_semaphore_signal((priority_map.begin()->second));
}
priority_unlocker(prio);
}
else
{
std::this_thread::sleep_for(std::chrono::milliseconds(50)); //test purpose only
counter++;
}
}
}
/*********************************************************************
*
* Function: void UART_access_manager::amInext(unsigned int prio)
*
* Description: check if current priority has to run next or not
*
* Notes: 0 is highest
*
* Returns: none
*
*********************************************************************/
void UART_access_manager::amInext(unsigned int prio)
{
if(priority_map.begin()->first == prio) dispatch_semaphore_signal(priority_map.begin()->second);
}
/*********************************************************************
*
* Function: void UART_access_manager::process_priority(unsigned
int prio)
*
* Description: TODO
*********************************************************************/
void UART_access_manager::process_priority(unsigned int prio)
{
printf("\n Priority_%d running \n",prio);
//TODO
}
/*********************************************************************
*
* Function: void UART_access_manager::priority_locker(unsigned
int prio)
*
* Description: lock mutex in a way to guarantee priority event
to
* get the fastest lock possible
*
* Notes: 0 is highest
******************************************************************/
void UART_access_manager::priority_locker(unsigned int prio)
{
//Low-priority threads: lock mtx, lock priority_0_mtx, lock data_mtx,
unlock priority_0_mtx,
//High-priority thread: lock priority_0_mtx, lock data_mtx, unlock
priority_0_mtx,
//this will insure that max priority level get privileged access to
the data
if(!prio)
{
priority_0_mtx.lock();
data_mtx.lock();
priority_0_mtx.unlock();
}
else
{
mtx.lock();
priority_0_mtx.lock();
data_mtx.lock();
priority_0_mtx.unlock();
}
}
/*********************************************************************
*
* Function: void
UART_access_manager::priority_unlocker(unsigned int prio)
*
* Description: unlock mtx based on the mutex locked by the
priority level
*
* Notes: 0 is highest
*
* Returns: none
*
*********************************************************************/
void UART_access_manager::priority_unlocker(unsigned int prio)
{
if(!prio)
{
data_mtx.unlock();
}
else
{
mtx.unlock();
data_mtx.unlock();
}
}
代码冗长易于理解,并且不会做任何事情,您进展顺利,将其视为一项作业,如果您有任何问题,请不要犹豫,评论,我会回复。
(HS:你想做什么?)
您将学到的东西:
std::multimap
标准::互斥
信号量
类