KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Shared Collection without Use...End use
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: March 29, 2023

Shared collection and shared objects always require a Use…end use to enclose any modification; however, for convenience, 4D automatically triggers an internal Use...end use on collection functions such as: push(), clear(), reverse(), etc..

Therefore, no explicit Use...end use is needed for the following:

$sharedCol:=New shared collection
$sharedCol2:=New shared collection("one"; "two")

$sharedCol2.push("three")
$sharedCol.push(3)
$sharedCol.push($sharedCol2)
$sharedCol.clear()
$sharedCol.push(1)

This built-in Use...end use is also triggered with read-only access to a property of a shared collection.

Note:
An explicit Use...end use must be used if there is any direct modification of element.

For example:
$sharedCol.push(5)
$sharedCol[0]:=9 // this will cause an error


Note:
For each operation on a shared object you are blocking access during that atomic function. A Use…end use may be beneficial if you want several modification to be performed atomically:

Use($sharedCol)
  $sharedCol.push(3)
  $sharedCol.push($sharedCol2)
  $sharedCol.clear()
  $sharedCol.push(1)
  $sharedCol[0]:=4 // No error
End use

Enclosing several modifications with one single Use...end use might have a performance benefit, because you only have a single lock/unlock pair, as opposed to several. The downside is if it takes the lock longer, then another process that is willing to update the same collection may have to wait longer.