Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Semaphores and Synchronized's (http://www.chiefdelphi.com/forums/showthread.php?t=72607)

byteit101 01-21-2009 07:03 PM

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?

gvarndell 01-21-2009 07:15 PM

Re: Semaphores and Synchronized's
 
Search through the WPILib sources, there are a few examples there.

byteit101 01-21-2009 07:19 PM

Re: Semaphores and Synchronized's
 
Where are the sources?

gvarndell 01-21-2009 07:31 PM

Re: Semaphores and Synchronized's
 
Quote:

Originally Posted by byteit101 (Post 805469)
Where are the sources?

http://first.wpi.edu/Images/CMS/Firs...1562Source.zip

gvarndell 01-21-2009 07:32 PM

Re: Semaphores and Synchronized's
 
http://first.wpi.edu/Images/CMS/Firs...1562Source.zip

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

Sean Raia 01-21-2009 07:41 PM

Re: Semaphores and Synchronized's
 
I think its pretty much like error in and error out. But im not sure

byteit101 01-21-2009 07:59 PM

Re: Semaphores and Synchronized's
 
and what is a SEM_ID?
(I have gotten this so far:
Code:

Synchronized s(???);
)

gvarndell 01-21-2009 08:03 PM

Re: Semaphores and Synchronized's
 
Quote:

Originally Posted by Sean Raia (Post 805487)
I think its pretty much like error in and error out. But im not sure

Huh?

byteit101 01-21-2009 08:33 PM

Re: Semaphores and Synchronized's
 
Quote:

Quote:

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

Re: Semaphores and Synchronized's
 
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/gla...ef/semLib.html

byteit101 01-21-2009 09:08 PM

Re: Semaphores and Synchronized's
 
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:
Code:

//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:
Code:

//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

Re: Semaphores and Synchronized's
 
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

Re: Semaphores and Synchronized's
 
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

Re: Semaphores and Synchronized's
 
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

Re: Semaphores and Synchronized's
 
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.


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

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