Go to Post Throw the programmers (or freshmen with duct tape depending on the state of your robot) in the trailer with the robot for the ride between events. Problem solved. :D - ASD20 [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
 
Thread Tools Rating: Thread Rating: 14 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 11-02-2010, 15:55
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
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
and you have Task B running:
Code:
CRITICAL_REGION(Sem2)
Wait(5.0);//demo purposes
CRITICAL_REGION(Sem1)
/*Code here*/
END_REGION
END_REGION
Assuming they were started within 5 seconds of each other, they will both lock, and nothing will happen. Why?
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
__________________
Bubble Wrap: programmers rewards
Watchdog.Kill();
printf("Watchdog is Dead, Celebrate!");
How to make a self aware robot: while (∞) cout<<(sqrt(-∞)/-0);
Previously FRC 451 (The Cat Attack)
Now part of the class of 2016 at WPI & helping on WPILib
Reply With Quote
  #2   Spotlight this post!  
Unread 11-02-2010, 16:27
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
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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


All times are GMT -5. The time now is 13:52.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi