View Single Post
  #12   Spotlight this post!  
Unread 23-02-2012, 18:53
jwakeman jwakeman is offline
Registered User
FRC #0063 (Red Barons)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2010
Location: 16510
Posts: 182
jwakeman is just really nicejwakeman is just really nicejwakeman is just really nicejwakeman is just really nicejwakeman is just really nice
Re: Help me understand PIDController::OnTarget()

Quote:
Originally Posted by wireties View Post
True but these class members are native machine word-sized thus changing them is an atomic operation in this context. It may not be desirable but it won't screw up.
I think you're correct that there is no danger of segmentation faults. However, if access to the variables is not synchronized then the potential for logic errors exist.

Example, in the following code the first if-statement could execute with the values of min/max input set to one set of values. The thread executing this code could then be preempted (depending on the thread priority and scheduling algorithm being used) by the application thread and the values of min/max input changed. The original thread would then continue executing the code with new values of min/max input causing undesireable results.

Code:
				
if (fabs(m_error) > (m_maximumInput - m_minimumInput) / 2)
    {
        if (m_error > 0)
       {
            m_error = m_error - m_maximumInput + m_minimumInput;
       }
       else
       {
           m_error = m_error + m_maximumInput - m_minimumInput;
       }
}
Reply With Quote