SharedMemoryLocks.jl

Lock object that can be shared between processes
Author andreyz4k
Popularity
0 Stars
Updated Last
1 Year Ago
Started In
May 2023

SharedMemoryLocks

Build Status

This package provides a class for a lock that can be shared between different processes on a local machine. It is a SpinLock that uses a SharedArray that is memory-mapped between processes, preserving atomicity. This lock is non-reentrant, meaning that recursive use will result in a deadlock.

Usage:

pids = addprocs(2)
@everywhere using SharedMemoryLocks

push!(pids, myid())
l = SharedMemoryLock(pids)
arr = SharedArray{Int}(1, init = 0, pids = pids)

function calc(l, arr)
    for i in 1:100
        lock(l)
        v = arr[1]
        sleep(0.000000001)
        arr[1] = v + 1
        unlock(l)
    end
end

f1 = @spawnat pids[1] calc(l, arr)
f2 = @spawnat pids[2] calc(l, arr)
fetch(f1)
fetch(f2)
@test arr[1] == 200

Required Packages

Used By Packages

No packages found.