Can two asynchronous tasks access the same array at the same time?
Can two asynchronous tasks access the same array at the same time?
Suppose you have an array that is modified by one asynchronous process and then you have another asynchronous process that reads from the same array. Modifications and reads are done synchronously. Now the question is, can the read process somehow gain access to the array while it is being modified by write process?
From experience I know that it can't and while synchronous operations are in progress the thread and therefore all the resources (including array) on which it operates will be blocked. Right? But is it logically consequent from the fact that JavaScript is single threaded or it is outlined specifically somewhere in the specs?
Do you have a specific problem or is this just a general question? If you have a specific problem should outline what that is
– charlietfl
Sep 2 at 12:43
As far as I remember the background-threading with worker is in proposal or beta. Like shared workers or web workers. But currently for your question Nodejs is single threaded and I dont think you cant share same variable access from different processes even with dependency injection. Use IPC to communicate changes to master and child processes for data sharing.
– Gary
Sep 2 at 12:57
You can use something like redis to share the variable across processes though which is a viable and frequently used solution
– Gary
Sep 2 at 13:02
2 Answers
2
Now the question is, can the read process somehow gain access to the array while it is being modified by write process?
No, JavaScript is single threaded. The only way to access the same data concurrently via JS is to use some shared ressources between two different JS scripts. On the web that can be WebWorkers with SharedArrayBuffers, on Nodejs some databases allow for concurrent mutations.
But is it logically consequent from the fact that JavaScript is single threaded or it is outlined specifically somewhere in the specs?
The specs do define threads, called Agents:
8.3 Agents
An agent comprises a set of ECMAScript execution contexts, an execution context stack, a running execution context, a
set of named job queues, an Agent Record, and an executing thread. Except for the executing thread, the constituents of
an agent belong exclusively to that agent.
That means all code runs on the same thread (job queue), all variables are only accessible from one thread (execution context). The only way to share memory between different agents (threads) is to use a SharedArrayBuffer, and you need different Agents (e.g. Webworkers).
Afaik the
Atomics
do define a thread model, but yes the rest is expected to be single-threaded.– Bergi
Sep 2 at 12:49
Atomics
Atomics seems interesting, (y) I didn't know about them.
– Adeel Imran
Sep 2 at 12:52
@bergi what is an Agent? I just found that in the specs and it looks like that could be related
– Jonas Wilms
Sep 2 at 12:56
@JonasWilms A generalisation of threads, I guess (but I haven't read the spec myself yet)
– Bergi
Sep 2 at 13:00
@jayarjo yup, sadly currently deactivated cause of Spectre...
– Jonas Wilms
Sep 2 at 13:27
One approach that I would do is make 3 arrays.
let kingArray = ;
let arrayForProcess1 = ;
let arrayForProcess2 = ;
someProcessOneExecution().then(response =>
if (kingArray.length === 0)
arrayForProcess1 = response;
else
doSomethingWithResponse(response);
);
someProcessTwoExecution().then(response =>
if (kingArray.length === 0)
arrayForProcess2 = response;
else
doSomethingWithResponse(response);
);
function doSomethingWithResponse()
// so something here..
This is not exactly what you asked, but the javascript event loop or call stack will execute them one by one. Since JS is single threaded. It will wait for these promises to resolve since they are I/O bases and as they get resolved. can be process1
or process2
. You can check them manually like in the approach above which is not the most optimal one.
process1
process2
Second Approach
One thing I would do in this case would be to chain those 2 promises together, and wait for them to be resolved one by one and update my array accordingly.
My own personal opinion (2 cents opinion) I wouldn't ever try to update the same array at the same time, if that is the usecase in this scenario maybe the problem execution strategy needs to be rethinked again.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Javascript executes on a single thread, i suggest you read up on how asynchronous operations work in javascript.
– Olian04
Sep 2 at 12:40