k = 10
n = 100
x = matrix(rnorm(n*k),n,k)
group = threadGroup("fit.group")
lock = threadLock("number of fit threads",num.threads = k, attach=2)
for(i in 1:k) {
thread(substitute(
fit[i] = lm(x,y), list(i = i)), data = list(x=x[,-i],y=x[,-i],
getLock(3);
num.threads = num.threads - 1;
yieldLock(3);
group=group)
}
# stop when the first thread finishes
getLock(lock, condition = substitute(num.threads < k,list(k=k))
cancel(group)