PDA

View Full Version : Semaphores and Synchronized's


byteit101
01-21-2009, 07:03 PM
The C_CPP_Programming_Guide.pdf talks about semaphores, but it does not give examples, could some one give me an example?

gvarndell
01-21-2009, 07:15 PM
Search through the WPILib sources, there are a few examples there.

byteit101
01-21-2009, 07:19 PM
Where are the sources?

gvarndell
01-21-2009, 07:31 PM
Where are the sources?

http://first.wpi.edu/Images/CMS/First/WorkbenchUpdateV1.2.1562Source.zip

gvarndell
01-21-2009, 07:32 PM
http://first.wpi.edu/Images/CMS/First/WorkbenchUpdateV1.2.1562Source.zip

Sorry if this is a double post.....

Sean Raia
01-21-2009, 07:41 PM
I think its pretty much like error in and error out. But im not sure

byteit101
01-21-2009, 07:59 PM
and what is a SEM_ID?
(I have gotten this so far:
Synchronized s(???);
)

gvarndell
01-21-2009, 08:03 PM
I think its pretty much like error in and error out. But im not sure

Huh?

byteit101
01-21-2009, 08:33 PM
Originally Posted by Sean Raia View Post
I think its pretty much like error in and error out. But im not sure
Huh?
You call semTake(semaphore id, wait time), the resource is locked, and then you release it by semGive(semaphore id);
I am trying to find out what the id is and how to create/get one

slavik262
01-21-2009, 08:53 PM
Semaphores basically prevent critical regions from executing at the same time. The Synchronized class basically handles semGive() and semTake() automatically. To mark the start of a critical region, you use the macro CRITICAL_REGION(semID) and END_REGION, which automatically wraps the area of code in a Synchronized class. To create a semaphore ID, simply declare a SEM_ID. Any critical regions that you do not want to run at once should share the semaphore ID. No further configuration of the semaphore ID is needed unless you want to use an advanced type of semaphore (such as a counting semaphore). More information can be found here:

http://www.slac.stanford.edu/exp/glast/flight/sw/vxdocs/vxworks/ref/semLib.html

byteit101
01-21-2009, 09:08 PM
slavik262, Thanks for the explanation, but i knew that from the C_CPP_Programming_Guide.pdf.
I did some more looking about, and I found the Answer:

//Top of file after includes
SEM_ID robotLock = semMCreate(SEM_Q_PRIORITY);//this creates a lock for the use of myRobot
//in a function somewhere else
CRITICAL_REGION(robotLock);
myRobot.Drive(1.0,0.0);
END_REGION;

the robotLock variable is the lock that needs to be obtained for a safe procedure, but it is not neccesary. So someone could do this://Top of file after includes
SEM_ID robotLock = semMCreate(SEM_Q_PRIORITY);//this creates a lock for the use of myRobot
//in a function somewhere else
CRITICAL_REGION(robotLock);
myRobot.Drive(1.0,0.0);
END_REGION;
//more code and other functions of a very complex nature here
myRobot.Drive(0.5,0.0);//this will compile but be not safe
So you need to explicitly obtain a lock for the variable you want and have decided to use for it.
Thanks for everyone's help in leading me to the right answer

Phazonmutant
01-29-2009, 11:43 PM
I've never worked with multithreading before; what would be some example uses of having multiple tasks that might both need to access data?

Joohoo
01-31-2009, 12:37 PM
Well Maybe you could have a thread that takes all the info from the camera, or some other processor intensive task and put it into a thread. Then you could use a semaphore to lock out the angle offset, or some other data. Then when the main thread goes to access the data when it is being written to by the seperate thread, you wont get invalid data because it is only half written to.

byteit101
01-31-2009, 10:27 PM
or, as we are using, a safety feature, press button 11, on thread 1, and it stops the motors for driving. though you dont have to use threads for that, but we thought it might be easier

Jared Russell
02-02-2009, 05:26 PM
If you want to get fancy, you can also look into the "semBCreate()" and "semCCreate()" functions. They offer slightly more advanced variations of the simple mutual exclusion semaphore.