2-877
2 Instruction Descriptions
NJ-series Instructions Reference Manual (W502)
Other Instructions
2
Lock and Unlock
• If Lock instructions are used in more than one task, a deadlock may occur if they are positioned
poorly. A Task Execution Timeout Error will occur if there is a deadlock and a total stop is performed.
• An error occurs in the following case. The value of Out does not change.
• There are more than 16,777,215 lock region at the same time.
Here, program P1 in task T1 and program P2 in task T2 both access the same global variable GTable1.
When the value of write request WriteReq changes to TRUE, P1 writes one record to record array
GTable1.Record[] and increments GTable1.Index. When read request ReadReq changes to TRUE, P2
decrements GTable1.Index and reads one record from GTable1.Record[].
The Lock instruction is used so that reading and writing do not occur at the same time.
Definition of Global Variable GTable
Data type
Global Variables
Sample Programming
Variable Data type Comment
USERTABLE STRUCT Record storage structure
Index INT Index
Record ARRAY[0..99] OF LREAL Record array
Variable Data type Initial value Comment
GTable1 USERTABLE (Index:=0,Record:=[100(0.0)]) Record storage structure
Task T1
Task T2
The priority of T1 is higher than the priority of T2,
so T2 is interrupted and execution of T1 is started.
Execution priority
High
Low
Lock(1) Lock(2)
Lock(2) Lock(1)
Execution interrupted.
Lock region
Normal execution
T1 executes a Lock instruction with an Index value
of 2. Therefore, execution of T1 is interrupted until
the Unlock instruction is executed in T2.
T2 executes a Lock instruction with an Index value of 1.
Therefore, execution of T2 is interrupted until the
Unlock instruction is executed in T1.
Here, restarting the execution of
both of the tasks must wait until
the Unlock instruction is executed
in the other task, i.e., they are
deadlocked.
Program P1
Task T1
GTabel1
Record 0
Record 1
:
Record n
Program P2
Task T2
Record
written.
Record
read.