Semaphores and Synchronized's

The C_CPP_Programming_Guide.pdf talks about semaphores, but it does not give examples, could some one give me an example?

Search through the WPILib sources, there are a few examples there.

Where are the sources?

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

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

Sorry if this is a double post…

I think its pretty much like error in and error out. But im not sure

and what is a SEM_ID?
(I have gotten this so far:

Synchronized s(???);

)

Huh?

[quote]Originally Posted by Sean Raia View Post

I think its pretty much like error in and error out. But im not sure

Huh?[/quote]

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

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

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

I’ve never worked with multithreading before; what would be some example uses of having multiple tasks that might both need to access data?

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.

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

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.