I am trying to have multiple snowfall threads write to the same file using write.table(). In a small number of cases, the rows are broken, i.e I observed that it looks like multiple rows mixed up, which I presume is when two threads try to write to the same file at the same time.
An example is :
require(snowfall)
sfInit(parallel = TRUE, cpus = 16)
sfLapply(1:10000,function(x){
mytable = data.frame(a = c(1,2,3),b = c(4,5,6))
write.table(mytable,"mytable.csv",sep = ',',append = T,col.names = F)
})
is there a way to ensure that only one thread writes to the file at a time, in essence a thread locks the file, writes to it and then releases the lock?