![]() |
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?
|
Re: Semaphores and Synchronized's
Search through the WPILib sources, there are a few examples there.
|
Re: Semaphores and Synchronized's
Where are the sources?
|
Re: Semaphores and Synchronized's
Quote:
|
Re: Semaphores and Synchronized's
|
Re: Semaphores and Synchronized's
I think its pretty much like error in and error out. But im not sure
|
Re: Semaphores and Synchronized's
and what is a SEM_ID?
(I have gotten this so far: Code:
Synchronized s(???); |
Re: Semaphores and Synchronized's
Quote:
|
Re: Semaphores and Synchronized's
Quote:
I am trying to find out what the id is and how to create/get one |
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 |
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 includesCode:
//Top of file after includesThanks for everyone's help in leading me to the right answer |
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?
|
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.
|
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
|
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