Learn R Programming

Rdsm (version 2.1.1)

rdsmlock: Lock/unlock operations.

Description

Lock/unlock operations to avoid race conditions among the threads.

Usage

rdsmlock(lck) rdsmunlock(lck)

Arguments

lck
Lock name, quoted.

Details

Standard lock/unlock operations from the threaded coding world. When one thread executes rdsmlock(), any other thread attempting to do so will block until the first thread executes rdsmunlock(). If a thread does rdsmlock() on an unlocked lock, the thread call immediately returns and the thread continues.

These functions are set in the call to mgrinit() via the argument boost to either boostlock and boostunlock() or backlock and backunlock(), depending on whether you set boost to TRUE or FALSE. respectively.

Code should be written so that locks are used as sparingly as possible, since they detract from performance.

Examples

Run this code

## Not run: 
# # unreliable function
# s <- function(n) {
#    for (i in 1:n) {
#       tot[1,1] <- tot[1,1] + 1
#    }
# }
# 
# library(parallel)
# c2 <- makeCluster(2)
# clusterExport(c2,"s")
# mgrinit(c2)
# mgrmakevar(c2,"tot",1,1)
# tot[1,1] <- 0
# clusterEvalQ(c2,s(1000))
# tot[1,1]  # should be 2000, but likely far from it
# 
# s1 <- function(n) {
#    require(Rdsm)
#    for (i in 1:n) {
#       rdsmlock("totlock")
#       tot[1,1] <- tot[1,1] + 1
#       rdsmunlock("totlock")
#    }
# }
# 
# mgrmakelock(c2,"totlock")
# tot[1,1] <- 0
# clusterExport(c2,"s1")
# clusterEvalQ(c2,s1(1000))
# tot[1,1]  # will print out 2000, the correct number
# ## End(Not run)

Run the code above in your browser using DataLab