OCCRA
Go to Post See, another reason for going--you can find out your chances of being thrown off the field for a safety violation! - EricH [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: 2 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 01-21-2009, 06:03 PM
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
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?
__________________
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 01-21-2009, 06:15 PM
gvarndell's Avatar
gvarndell gvarndell is offline
Software Engineer
AKA: Addi's and Georgie's Dad
FRC #1629 (GaCo)
Team Role: Parent
 
Join Date: Jan 2009
Rookie Year: 2008
Location: Grantsville, Maryland
Posts: 350
gvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond repute
Re: Semaphores and Synchronized's

Search through the WPILib sources, there are a few examples there.
Reply With Quote
  #3   Spotlight this post!  
Unread 01-21-2009, 06:19 PM
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: Semaphores and Synchronized's

Where are the sources?
__________________
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
  #4   Spotlight this post!  
Unread 01-21-2009, 06:31 PM
gvarndell's Avatar
gvarndell gvarndell is offline
Software Engineer
AKA: Addi's and Georgie's Dad
FRC #1629 (GaCo)
Team Role: Parent
 
Join Date: Jan 2009
Rookie Year: 2008
Location: Grantsville, Maryland
Posts: 350
gvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond repute
Re: Semaphores and Synchronized's

Quote:
Originally Posted by byteit101 View Post
Where are the sources?
http://first.wpi.edu/Images/CMS/Firs...1562Source.zip
Reply With Quote
  #5   Spotlight this post!  
Unread 01-21-2009, 06:32 PM
gvarndell's Avatar
gvarndell gvarndell is offline
Software Engineer
AKA: Addi's and Georgie's Dad
FRC #1629 (GaCo)
Team Role: Parent
 
Join Date: Jan 2009
Rookie Year: 2008
Location: Grantsville, Maryland
Posts: 350
gvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond repute
Re: Semaphores and Synchronized's

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

Sorry if this is a double post.....
Reply With Quote
  #6   Spotlight this post!  
Unread 01-21-2009, 06:41 PM
Sean Raia's Avatar
Sean Raia Sean Raia is offline
Webapp Dev & Security Analyst
no team (Anonymous)
Team Role: Mentor
 
Join Date: Oct 2008
Rookie Year: 2008
Location: NY
Posts: 467
Sean Raia has a reputation beyond reputeSean Raia has a reputation beyond reputeSean Raia has a reputation beyond reputeSean Raia has a reputation beyond reputeSean Raia has a reputation beyond reputeSean Raia has a reputation beyond reputeSean Raia has a reputation beyond reputeSean Raia has a reputation beyond reputeSean Raia has a reputation beyond reputeSean Raia has a reputation beyond reputeSean Raia has a reputation beyond repute
Re: Semaphores and Synchronized's

I think its pretty much like error in and error out. But im not sure
Reply With Quote
  #7   Spotlight this post!  
Unread 01-21-2009, 06:59 PM
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: Semaphores and Synchronized's

and what is a SEM_ID?
(I have gotten this so far:
Code:
Synchronized s(???);
)
__________________
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
  #8   Spotlight this post!  
Unread 01-21-2009, 07:03 PM
gvarndell's Avatar
gvarndell gvarndell is offline
Software Engineer
AKA: Addi's and Georgie's Dad
FRC #1629 (GaCo)
Team Role: Parent
 
Join Date: Jan 2009
Rookie Year: 2008
Location: Grantsville, Maryland
Posts: 350
gvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond repute
Re: Semaphores and Synchronized's

Quote:
Originally Posted by Sean Raia View Post
I think its pretty much like error in and error out. But im not sure
Huh?
Reply With Quote
  #9   Spotlight this post!  
Unread 01-21-2009, 07:33 PM
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: 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
__________________
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
  #10   Spotlight this post!  
Unread 01-21-2009, 07:53 PM
slavik262's Avatar
slavik262 slavik262 is offline
We do what we must because we can.
AKA: Matt Kline
FRC #0537 (Charger Robotics)
Team Role: Alumni
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Sussex, WI
Posts: 310
slavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to behold
Send a message via AIM to slavik262
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
__________________

Last edited by slavik262 : 01-21-2009 at 07:55 PM.
Reply With Quote
  #11   Spotlight this post!  
Unread 01-21-2009, 08:08 PM
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: 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
__________________
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
  #12   Spotlight this post!  
Unread 01-29-2009, 10:43 PM
Phazonmutant's Avatar
Phazonmutant Phazonmutant is offline
Winrar
AKA: Greg Mitchell
FRC #2556 (RadioActive Roaches)
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2008
Location: Niceville, FL
Posts: 17
Phazonmutant is on a distinguished road
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?
Reply With Quote
  #13   Spotlight this post!  
Unread 01-31-2009, 11:37 AM
Joohoo's Avatar
Joohoo Joohoo is offline
Registered User
FRC #0340 (G.R.R.)
Team Role: Mentor
 
Join Date: Jun 2005
Rookie Year: 2005
Location: Rochester
Posts: 242
Joohoo is just really niceJoohoo is just really niceJoohoo is just really niceJoohoo is just really niceJoohoo is just really nice
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.

Last edited by Joohoo : 01-31-2009 at 11:40 AM.
Reply With Quote
  #14   Spotlight this post!  
Unread 01-31-2009, 09:27 PM
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: 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
__________________
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
  #15   Spotlight this post!  
Unread 02-02-2009, 04:26 PM
Jared Russell's Avatar
Jared Russell Jared Russell is offline
in hibernation
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,278
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
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.
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
Bill of Materials, do it and do it well, and bring it to your event sanddrag Rules/Strategy 3 03-24-2006 08:09 PM
pic: Stars and Stripes over Einstein and Galileo CD47-Bot Extra Discussion 6 04-30-2004 11:54 PM


All times are GMT -5. The time now is 09:43 PM.

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