Saturday 29 December 2018

Multithreading (CyclicBarrier) in Node.js

Hi all,I have a question regarding multithreading (I know it's technically one thread) in Node.js. Let's say I have thread A, which at some point splits off into A and B. At some point in the future, A and B merge into one thread (A) again. But here is the issue: if subthread A finishes its work first, it can add a .on('event') to B's EventEmitter, and that's easy to work with. But what if B finishes its work before A? In this hypothetical scenario, by the time A registers the .on('event) listener, the event was already emitted, and thus A would have missed it, resulting in the program locking up. My current workaround is checking for a boolean being true first, and only after that registering the listen event, but 1) that is ugly; and 2) it is not 100% safe due to the lack of mutex in Node.js. Am I missing something here? Is someone willing to point me in the right direction?

Submitted December 29, 2018 at 02:06PM by NullProbability

No comments:

Post a Comment