Reputation: 770
I am currently working with Web Workers and for some testing purposes it would be nice if it was possible to wait in the main thread until workers have finished what they're doing. So:
Main thread -> start workers
Main thread -> wait for workers to finish
workers -> do some work
Workers finish.
Main thread -> do some stuff.
I realize the most correct way of doing this is using a callback in the main thread that is called whenever a worker finishes but this will not work for practical reasons. I've made what is similar to a CyclicBarrier in Java but whenever I call barrier.enter() in the main thread I of course get an error. Hence what I want to know is if there's any setting or something similar I can set which temporarily allows me to block on the main thread. I'm running my program using Node btw.
This is purely for testing purposes, I would of course never use this in a production environment!
Upvotes: 2
Views: 1798
Reputation: 53139
If you insist on not using events, you're putting yourself in a tricky situation. If you block the main thread, you make it impossible to process event that the worker sends once it's done. And there's no other communication channel between worker and main thread.
Or is there? You could possibly create a HTTP server that would relay all data from worker to the main thread. Main thread could create a synchronous HTTP request on that server. Synchronous AJAX requests do block main thread but do not eat the CPU.
Upvotes: 0