Controlling critical section

The following example shows how to use a mutex object to control accesses to the critical section.

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

#define TASK_COUNT 4

HANDLE MutexHandle;


ERROR Task(PVOID Arg)
{
  /* Infinite loop */
  while(1)
  {
    /* Acquire the mutex */
    osWaitForObject(MutexHandle, OS_INFINITE);

    /* Suspend task execution for one second */
    printf("Task %i is running.\n", (int) Arg);
    osSleep(AR_TICKS_PER_SECOND);

    /* Release mutex */
    osReleaseMutex(MutexHandle);
  }
}


int main(void)
{
  int i;

  /* Initialization */
  arInit();
  stInit();
  osInit();

  /* Create objects */
  MutexHandle = osCreateMutex(NULL, FALSE);
  for(i = 0; i < TASK_COUNT; i++)
    osCreateTask(Task, (PVOID) i, 0, 0, FALSE);

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

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