semInit (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

ALL

Firmware version:

1.00 / 1.00.00


The semInit() function will initialize a SEMAPHORE variable with the specified initial value.

The semInit() function must be called before any other operations can be performed on the semaphore.

 

 

Input:

initval : INT

The initial value for the semaphore.

 

Returns: SEMAPHORE

The initialized semaphore.

 

Declaration:

FUNCTION semInit : SEMAPHORE;
VAR_INPUT
  initval : INT := 1;
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
 
VAR
  sem : SEMAPHORE;
END_VAR;
 
PROGRAM test;
  ...
  // Initialize Semaphore
  sem := semInit(initval:=3);
  IF semValue(sem:=sem) = -1 THEN DebugMsg(message:="sem failed to init!"); END_IF;
  ...
BEGIN
  ...
  // Wait until resource is free or timeout
  IF semWait(sem:=sem,timeout:=1000) = 0 THEN
    // Only do these actions if we have access to the resource
     ...
    // Free the resource after use
    semSignal(sem:=sem);
  END_IF;
  ...
END;
 
END_PROGRAM;