The mxUnlock() function will unlock a MUTEX variable previously locked by the mxLock() function.
A mutex can only be unlocked by the owner of the mutex.
The mutex mechanism is traditionally used for implementing critical sections. Also see the section on thread syncronization for more information.
Input:
mx : MUTEX
The MUTEX to unlock.
Returns: INT
0
|
Mutex is unlocked.
|
1
|
Mutex is not initialized.
|
2
|
Mutex is locked by another thread.
|
Declaration:
FUNCTION mxUnlock : INT;
VAR_INPUT
mx : MUTEX;
END_VAR;
Example:
INCLUDE rtcu.inc
VAR
mxCnt : MUTEX;
Count : DINT := 0;
END_VAR;
THREAD_BLOCK Thread_A;
WHILE TRUE DO
mxLock(mx:=mxCnt);
Count := Count + 1;
mxUnlock(mx:=mxCnt);
END_WHILE;
END_THREAD_BLOCK;
THREAD_BLOCK Thread_B;
WHILE TRUE DO
mxLock(mx:=mxCnt);
Count := Count + 5;
mxUnlock(mx:=mxCnt);
END_WHILE;
END_THREAD_BLOCK;
PROGRAM test;
VAR
TA : Thread_A;
TB : Thread_B;
i : INT;
END_VAR;
mxCnt := mxInit();
TA();
TB();
BEGIN
Sleep(delay:=1000);
DebugFmt(message:="Count: \4",v4:=Count);
END;
END_PROGRAM;
|