|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools |
Rating:
|
Display Modes |
|
|
|
#1
|
||||
|
||||
|
Re: Task Creation and Semaphore Tutorial
Unless you have fancy code, you should not encounter this (for the most part), but watch out for race conditions and locking.
if I have task A and it is running: Code:
CRITICAL_REGION(Sem1) Wait(5.0);//demo purposes CRITICAL_REGION(Sem2) /*Code here*/ END_REGION END_REGION Code:
CRITICAL_REGION(Sem2) Wait(5.0);//demo purposes CRITICAL_REGION(Sem1) /*Code here*/ END_REGION END_REGION Task A locks Sem1 and waits for 5 seconds, where Task B starts, and Locks Sem2 Task A has waited 5 seconds, and tries to lock Sem2, but Task B has it, so It waits for B to release it. Meanwhile, B has waited its 5 seconds, and tries to Lock Sem1, but Task A has it so it waits for A to release it. they are each waiting for the other to do something, so they do nothing |
|
#2
|
||||
|
||||
|
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 |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| printf not working and UserProgram_StartupLibraryInit task deleted | John Young | C/C++ | 4 | 08-02-2010 21:36 |
| semaphore of the trailer | frcchile | Technical Discussion | 3 | 26-01-2009 13:56 |
| New LabVIEW TipJar Tutorial Video: Scalability, Modularity and Style. | LVMastery | NI LabVIEW | 0 | 18-01-2009 14:56 |
| Question about 3rd Tutorial ETA and FRC Modeler Motor Config | Ted155 | LabView and Data Acquisition | 2 | 21-01-2007 19:45 |