I'm working with mclapply
from the multicore
package (on Ubuntu), and I'm writing a function that required that the results of mclapply(x, f)
are returned in order (that is, f(x[1]), f(x[2]), ...., f(x[n])
).
# multicore doesn't work on Windows
require(multicore)
unlist(mclapply(
1:10,
function(x){
Sys.sleep(sample(1:5, size = 1))
identity(x)}, mc.cores = 2))
[1] 1 2 3 4 5 6 7 8 9 10
The above code seems to imply that mclapply
returns results in the same order as lapply
.
However, if this assumption is wrong I'll have to spend a long time refactoring my code, so I'm hoping to get assurance from someone more familiar with this package/parallel computing that this assumption is correct.
Is it safe to assume that mclapply
always returns its results in order, regardless of the optional arguments it is given?