I recently tried to protect the access to some data in structured text using a mutex, as implemented in the function block FB_IecCriticalSection. As i understand this documentation the scheduler would block any task trying to execute fbCriticalSection.Enter() if the critical section cannot be entered:
If another task tries to access an occupied critical area through an Enter() call, it is blocked by the TwinCAT scheduler. The task is blocked until the section is enabled again! Once enabled, processing of the program code continues, and the critical section is entered.
Yet in all example code I find it to be used like this:
IF fbCrititcalSection.Enter() THEN
(* start of critical section *)
(* end of critical section *)
fbCrititcalSection.Leave();
END_IF
Is there any reason for the usage of the IF statement? If I understand correctly, the critical section would not be entered as long as the section is locked by another task, even if the IF statement is omitted, so to me it seems to serve no purpose.
Thanks!