title: Multithreading in microcontrollers
body: <p>I have a <strong><code>volatile unsigned char array LedState[5]</code></strong> variable which is shared between threads. Each index in the array denotes a state. According to each state, the LED will flash in different sequences. One thread sets the state in the array and another thread based on the array index will flash the LEDs.</p>
  
  <pre><code>void TurnOnled(state) {
     LedState[state] =1;
  }
  void TurnOffLed(state) {
     LedState[state] = 0;
  }
  int CheckLedState(state) {
     return LedState[state]? 1 : 0;
  }
  Thread 1
  ---------
  TurnOnLed(3);
  /*Set of instructions*/
  TurnOffLed(3);
  
  Thread 2
  --------
  if (CheckLedState(3)) {
     /*Flash LEDS according to state*/
  else {/*do nothing*/}
  </code></pre>
  
  <p>The problem I have is sometimes in thread 1, I need to <code>TurnOnLed</code> and <code>TurnOffLed</code> immediately. How can I ensure Thread 2 sees the <code>TurnOnLed</code> before  <code>TurnOffLed</code> gets called. The above is just a simple example but in reality the LedState variable is set and unset from more than one thread. But no same state is set by different threads.</p>