Using a manual-reset event

The following example demonstrates the implementation and management of a manual-reset event to coordinate activities across multiple tasks.

#include <stdio.h>
#include "OS_API.h"


UINT32 Counter;
HANDLE EventHandle;


ERROR Task1(PVOID Arg)
{
  /* Mark parameter as unused */
  AR_UNUSED_PARAM(Arg);

  /* Infinite loop. Increment counter every time, when event is set (process
     is turned on) */
  while(1)
  {
    osWaitForObject(EventHandle, OS_INFINITE);
    Counter++;
  }
}


ERROR Task2(PVOID Arg)
{
  /* Mark parameter as unused */
  AR_UNUSED_PARAM(Arg);

  /* Turn on/off the process on every second */
  while(1)
  {
    /* Turn on the process */
    osSetEvent(EventHandle);
    osSleep(AR_TICKS_PER_SECOND);

    /* Turn off the process */
    osResetEvent(EventHandle);
    osSleep(AR_TICKS_PER_SECOND);
  }
}


ERROR Task3(PVOID Arg)
{
  /* Mark parameter as unused */
  AR_UNUSED_PARAM(Arg);

  /* Print the counter value */
  while(1)
    printf("%lu\n", Counter);
}


int main(void)
{
  /* Initialization */
  arInit();
  stInit();
  osInit();

  /* Initialize global data */
  Counter = 0;

  /* Create objects */
  EventHandle = osCreateEvent(NULL, FALSE, TRUE);
  osCreateTask(Task1, NULL, 0, 0, FALSE);
  osCreateTask(Task2, NULL, 0, 0, FALSE);
  osCreateTask(Task3, NULL, 0, 0, FALSE);

  /* Start the operating system */
  osStart();

  /* Deinitialization */
  osDeinit();
  arDeinit();
  return 0;
}
SpaceShadow documentation