Replies: 1 comment 5 replies
-
Hey @aweiu! |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Why?
In my application, the original solution was
postMessage
+on('message')
. Unfortunately, they are too slow for my application! High frequency + large objects are not suitable for IPC. Hence the idea of driving messages based on the objectbuffer.The first problem to solve: How to notify other threads immediately when the sharedObject changes?
1. Based on Atomics.waitAsync + Atomics.notify?
The test shows that when the worker executes
Atomics.notify
, the main thread can indeed end the waiting forAtomics.waitAsync
. But once the Worker thread has high-frequency shared data changes, the main thread will miss the notification. For example:For the continuous two shared data changes above, the main thread can only receive the first notification. The reason is that the second notify was issued before the main thread entered the next waitAsync loop. So if it is just to get notification of changes faster, is
while(true)
+waitAsync
not as good as directlysetInterval 0
+read sharedMessage
? Like this:The above is to discuss the listening scheme alone. Of course
setInterval 0
will also miss changes. Is there a better solution? Looking forward to everyone's discussion!The next problem to solve is how not to miss data changes?
I think it's time for us to use a message queue, just like IPC's postMessage. Here is an example:
I wonder this message queue-based solution should not have thread safety issues, because: when producing, always push, when consuming, always shift. In the above example, the expected output of the main thread should always be: msg1, msg2, msg1, msg2... But actually, it's wrong to read it, the actual result is as shown:
The cause of this problem may still be due to simultaneous reading and writing of shared data. I also noticed an issue the author previously mentioned, I wonder if it's because of it: #60
It seems that the method of
push
+shift
is also not feasible now. I am preparing to try Circular_buffer. By the way, I would like to ask: assuming that push + shift is feasible, this kind of operation that frequently causes the length of sharedArray to change, isn't it less efficient than Circular_buffer on performance?Looking forward to the author's reply and everyone's discussion and suggestions! @Bnaya
Beta Was this translation helpful? Give feedback.
All reactions