Description Usage Arguments Details Author(s) Examples
Lock/unlock operations to avoid race conditions among the threads.
1 2 | rdsmlock(lck)
rdsmunlock(lck)
|
lck |
Lock name, quoted. |
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.
Norm Matloff
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.