lock()

Read(1937) Label: synchronization lock,

Description:

Use a synchronization lock.

Syntax:

lock(n,s)

Note:

The function enforces limits on access to a file in an multithreading execution environment using lock-based synchronization. With the lock, only one thread can access and process the file at a time.

Parameter:

n

Lock name

s

Timeout (in millisecond); waiting to be locked if the parameter is absent

Option:

@u

Unlock the restriction

Return value:

Lock name

Example:

  Locking:

 

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 1000 miliseconds.

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.dfx

 

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()