Description:
Use a synchronization lock.
Syntax:
lock(n,s)
Note:
The function enforces limits on access to a file in a multithreading execution environment using lock-based synchronization. With the lock, only one thread can access and process the file at a time. Return true if the locking succeeds and return false if it fails.
Parameter:
n |
Lock name |
s |
Timeout (in millisecond); waiting to be locked if the parameter is absent |
Option:
@u |
Unlock the restriction |
Return value:
Boolean
Example:
Locking; below is the script file D:\test \lock1.splx:
|
A |
|
1 |
=lock(1) |
Use a lock named 1. |
2 |
>output("Task "/arg1/" start...") |
Output the parameter and start to calculate. |
3 |
=file("D:/file/test/1.btx") |
Create a btx file. |
4 |
=create(ID,Value) |
Create a table sequence. |
5 |
>A4.record([arg1,arg1*arg1]) |
Add records to A4’s table sequence. |
6 |
>sleep(1000) |
Make the system sleep for 1,000 milliseconds. |
7 |
>A3.export@ab(A4) |
Write A4’s table sequence into the btx file. |
8 |
>output("Task "/arg1/" finished.") |
Output the parameter and finish the calculation. |
9 |
>lock@u(1) |
Unlock the access restriction. |
File access:
|
A |
|
1 |
=file("D:/file/test/1.btx") |
|
2 |
D:\test \lock1.splx |
|
3 |
=callx(A2,to(100)) |
When using callx() function to acess a file, the max number of parallel task set in Tool -> Options should be greater than 1 in case that the lock-based synchronization reports an error. |
4 |
=A1.import@b() |
|