Synchronized Functions

sync(PnutsFunction func {, Object lock } )

sync() returns a function that locks the specified object lock and calls func function. When the function returns, the lock is unlocked. If lock is not specified, func is used as a lock.

Although the function mutex() can describe fine grained synchronization with two simple operations; lock and unlock, sync() is easier to use in the following situations.

e.g.
file = File("foo")

function update(f){
  w = open(f, "w")
  w.write(...)
  w.close()
}

rfunc = sync(read, file)
wfunc = sync(update, file)

rfunc(open(file))
wfunc(file)

Back