0

I am using async.js to work on an array that holds roughly 12'000 values. The problem I'm currently facing is that I am not 100% certain what I can take for granted from async.js. First let me show you an abstract of the iterator function im using (coffeescript):

baseFace = 0
dataBuffer = new DataBuffer() # my own class that wraps DataView on an Uint8Array
async.each(@blocks, 
     (block, callback) =>
          numFaces = writeToBuffer(dataBuffer, block, baseFace)
          baseFace += numFaces
          callback(null)
      ,
      () =>
          console.log 'done'
)

So essentially the order in which the blocks are written to the buffers is unimportant, but there are two things I need to be assured of:

  • The entire writeToBuffer has to be done atomic, its not reentrant since it writes to the end of the given buffer and the block has to be written as one (multiple calls to DataView.setXXXX)

  • The baseFace variable too must be accessed atomically inside the function. it cant be that writeToBuffer is called for another element before the face count of the previous call to writeToBuffer has been added to baseFace

Basically you can say my iterator cannot be evaluated multiple times at the same time, not even with interleaving (like, writeToBuffer, writeToBuffer, baseFace += numFaces, baseFace += numFaces). Coming from C++/C# etc I always fear that something goes wrong when methods access data like in the above example.

Thank you for any insight or tips about that topic, Cromon

Cromon
  • 821
  • 10
  • 24
  • Your `(block, callback) =>` function is not actually asynchronous. – Bergi Jan 10 '15 at 22:01
  • So that means my two assumptions are both true? I dont have to worry about interleaving of commands from the iterator? It does not have to be asynchronous, with async.each i can just process the 12k elements without blocking the rendering thread, this already works. – Cromon Jan 10 '15 at 22:36
  • I don't know what `writeToBuffer` does. Is it async or not? It doesn't seem to have a callback. Synchronous code is always executed "atomically", as JS is single-threaded. You should not use `async.js`, but a plain loop. – Bergi Jan 10 '15 at 22:53
  • `writeToBuffer` is entirely synchronous. All it does is doing some calculations on the block and a bunch of DataView.setUint8/DataView.setUint32. I know that javascript is per se single threaded but i wasnt sure if it does some sort of interleaving, you know, like execute a bit of task A, execute a bit of task B, etc which would mean that my code could be executed multiple times. Right now everything is working perfect, i just wasnt sure if this is guaranteed to be like that. If you wish you can put your answer here and ill accept it! – Cromon Jan 10 '15 at 23:16
  • Yes, it does interleave asynchronous tasks, one callback after the other. But all ([except some host functions](http://stackoverflow.com/q/2734025/1048572)) synchronous code (including asynchronously called callbacks) is entirely thread-safe. – Bergi Jan 11 '15 at 11:12

0 Answers0