View Single Post
  #3   Spotlight this post!  
Unread 02-11-2010, 04:27 PM
taichichuan's Avatar
taichichuan taichichuan is offline
Software Mentor
AKA: Mike Anderson
FRC #0116 (Epsilon Delta)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2010
Location: Herndon, VA
Posts: 328
taichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud of
Send a message via AIM to taichichuan
Re: Task Creation and Semaphore Tutorial

Be careful of using binary semaphores (via semBCreate) for mutual exclusion. There is a rather nasty problem known as priority inversion that can happen if you're not careful.

Binary semaphores are best used for synchronization like:

Forever() {
wait here until something happens;
do what must be done;
}

And in another piece of code:

signal that something must be done

That is, binary semaphores are usually found as a single instance (semTake in VxWorks or the synchronize method in the WPILib) and a semGive is located in another piece of code.

For mutual exclusion (i.e., to protect a piece of data from simultaneous access) the declaration would look more like:

m_mySemaphore = semMCreate(SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE);

Then the use of the semaphore is more consistent with the WPILib

CRITICAL_REGION(m_mySemaphore);
...
END_REGION

construct.

Using a binary semaphore for mutual exclusion is what got NASA into trouble on Mars with the first rover (before Spirit, et al). They had to create a patch and use interpanetary FTP to upload it. Then they pressed the reset button from Earth and held their breath... Fortunately, it worked.

HTH,

Mike
Reply With Quote