:global.trans/2 coordinates a cluster-wide critical section: it acquires a distributed lock, runs a function, and releases the lock afterward. A lock identifier such as {:some_resource, self()} distinguishes the requester; using a process ID allows at most one process to hold that lock. section 12.1.5
Serializing large-data processing
def process(large_data) do :global.trans( {:some_resource, self()}, fn -> do_something_with(large_data) end ) end This serializes the operation across the cluster without first sending the large data to a synchronization process. section 12.1.5
Use locks judiciously: excessive locking can produce deadlocks, livelocks, or starvation. Process-based synchronization is generally easier to reason about, while a lock can avoid the serialization, transmission, and copying costs of passing large messages between nodes. section 12.1.5
def process(large_data) do
:global.trans( #1
{:some_resource, self},
fn ->
do_something_with(large_data) #2
end
)
end
View the runnable Elixir-shaped acquire, execute, and release pattern. section 12.1.5
