10

I know one can either splice an item out of an array, or delete it with delete. The former approach can cause concurrency problems, e.g. if one thread is walking over the array while another has just shifted or spliced. delete doesn't have this issue if forEach is used on the array, since forEach will walk over holes in the array.

However, the array can't keep growing forever and will necessitate sweeping, potentially causing the same issue as in the case of a splice. Sounds like I need locking, but I'd be amused if Javascript had any facilities for it. Any thoughts?

dmkc
  • 1,161
  • 1
  • 15
  • 22
  • 2
    JavaScript doesn't support threads, other than a recent HTML5 feature (and that doesn't allow sharing of arrays) – Dave May 04 '13 at 16:32
  • 1
    Can you show us an example where two "threads" are trying to access an array at the same time? It should be impossible with JavaScript – Bergi May 04 '13 at 16:33
  • 1
    [You shell not use `delete` on arrays](http://stackoverflow.com/q/500606/1048572) – Bergi May 04 '13 at 16:37
  • @dystroy: I know it *is* (though [some oddities](http://stackoverflow.com/q/2734025/1048572)), but the OP sounded like he had an actual problem with concurrency… – Bergi May 04 '13 at 16:43
  • 2
    Over-thinking a problem that doesn't exist? :) – deceze May 04 '13 at 16:55

2 Answers2

13

Regarding your precise question: No, you can't have concurrency problem as JavaScript isn't multithreaded. Even if you use webworkers you won't have problems as no data is shared (workers communicate by passing messages). Even in node.js your script isn't multi-threaded. There's no way that a parallel execution does anything during the execution of your function unless you use await.

So simply use splice, there is no need to lock the array.

Regarding concurrency problems more generally, you should be aware that, as soon as you use await, the execution can be cut in chunks and another function can run while you're awaiting. splice will never be cut but be careful to the logic of your algorithm on shared data when you're in an async function.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • That makes sense in light of `libev` that node.js uses. I should have researched this a bit more before asking this! Thanks. – dmkc May 05 '13 at 01:23
  • With ECMA6, JavaScript is asynchronous with help of Promises. We can create async functions now. So don't you think that we might need a lock ? – Jay Shah Jan 26 '18 at 13:20
  • @JayShah there are some cases where you could have interleaved executions thanks to async/await but not during the execution of a splice statement. If some block of code isn't doing asynchronous calls, it won't be broken and no other user code will be executed at the same time. – Denys Séguret Jan 26 '18 at 14:30
  • This isn't true. Yes - JS isn't multithreaded. But not being multithreaded doesn't mean you cannot have concurrency problems. Having two concurrent events call a function that loops + splices the same large array at the same time causes an undefined logical flow which could, and often does, lead to overflows. Example problem here: https://stackoverflow.com/questions/9882284/looping-through-array-and-removing-items-without-breaking-for-loop – aggregate1166877 Oct 14 '21 at 01:41
  • @aggregate1166877 This function you revived was asked and answered in 2013, before `async`. I edited to precise a potential problem. But your comment is very vague and *"Having two concurrent events call a function"* is misleading as, whatever the model, the calls are sequential and the only parallelism is interleaved chunks of execution in async function. – Denys Séguret Oct 14 '21 at 06:46
  • 1
    My apologies @DenysSéguret, I had a pretty bad bug in a large application that looked like a concurrency issue. After over a week of debugging and not finding the problem, I was now trying to trying to reproduce it in a minimal code example for an SO question when the minimal example suddenly made the issue glaringly obvious. It had nothing to do with event loop concurrency. – aggregate1166877 Oct 14 '21 at 09:39
1

Javascript is singlethreaded so there's no problem.

Michal Borek
  • 4,584
  • 2
  • 30
  • 40