Now you can download a copy of these docs so you can use them offline! Download now
Synchronized Class Reference

#include <Synchronized.h>

Public Member Functions

 Synchronized (SEM_ID)
 
 Synchronized (ReentrantSemaphore &)
 
virtual ~Synchronized ()
 

Detailed Description

Provide easy support for critical regions.

A critical region is an area of code that is always executed under mutual exclusion. Only one task can be executing this code at any time. The idea is that code that manipulates data that is shared between two or more tasks has to be prevented from executing at the same time otherwise a race condition is possible when both tasks try to update the data. Typically semaphores are used to ensure only single task access to the data.

Synchronized objects are a simple wrapper around semaphores to help ensure that semaphores are always unlocked (semGive) after locking (semTake).

You allocate a Synchronized as a local variable, not on the heap. That makes it a "stack object" whose destructor runs automatically when it goes out of scope. E.g.

{ Synchronized _sync(aReentrantSemaphore); ... critical region ... }

Definition at line 82 of file Synchronized.h.

Constructor & Destructor Documentation

Synchronized::Synchronized ( SEM_ID  semaphore)
explicit

Synchronized class deals with critical regions. Declare a Synchronized object at the beginning of a block. That will take the semaphore. When the code exits from the block it will call the destructor which will give the semaphore. This ensures that no matter how the block is exited, the semaphore will always be released. Use the CRITICAL_REGION(SEM_ID) and END_REGION macros to make the code look cleaner (see header file)

Parameters
semaphoreThe semaphore controlling this critical region.

Definition at line 17 of file Synchronized.cpp.

Synchronized::~Synchronized ( )
virtual

This destructor unlocks the semaphore.

Definition at line 32 of file Synchronized.cpp.


The documentation for this class was generated from the following files:

Generated on Sat Apr 26 2014 12:26:47 for WPILibC++ by doxygen 1.8.6