rdsmlock: Lock/unlock operations.

Description Usage Arguments Details Author(s) Examples

View source: R/Rdsm.R

Description

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

Usage

1
2
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.

Author(s)

Norm Matloff

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
## 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)

Rdsm documentation built on May 1, 2019, 10:52 p.m.