Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm building a javascript application that does continuous drawing, but relies on asynchronous scripts in the background which also need to poll for events and write to "global variables". This got me to start using Workers, but they can't access my global variables consistently. I was thinking of using Singletons to communicate between the two, allowing my draw loop to keep running while my workers do the heavy lifting.

Theoretically this seems to work, but since workers are designed to operate more functionally is there a better approach?

More info if needed: Essentially, I'm polling for keyboard interrupts similar to how operating systems register interrupts to programs. Because of scripts created/compiled/stored remotely, scripts are being inserted on the fly with Ensure and run by workers, but they still need to access my global variables my draw loop uses.

share|improve this question
    
I'm not even sure if Singletons will work. I may have to rewrite the whole thing purely functional. Proving that would be necessary is also an acceptable answer. –  David R. Sep 16 at 22:26
    
If you're doing event polling, do you think it would make sense to have your draw loop check your events and then toss a message over to the web worker at that point? I think if you could provide even more details it would be helpful for this question... –  J Trana Sep 17 at 1:08
    
Passing in an array of interrupts wouldn't be a problem. The real question is: is using A Singleton inside a Web Worker bad form or is that an acceptable solution? The crux is Singletons are naturally thread safe, but javascript isn't strongly typed or restricted to classes. –  David R. Sep 17 at 9:50
    
At least within other paradigms, Singletons (by which I mean having only one instance of a class) are not naturally thread safe: you can have two or more threads acting on the same object at the same time and can still have problems. In Javascript you can mimic the Singleton pattern. However, the issue of thread safety and indeed threads is not handled the same way. Instead of using global variables and mutexes, you use web workers and message passing. Each worker (plus the "main thread") sort of acts as a single thread of execution; I don't think you can (should?) use objects across "threads" –  J Trana Sep 18 at 1:45
    
You're right, it just turns out my previous Singleton implementations were thread safe because that's how I was taught to do them. Since web workers aren't technically threads, I'm not sure what terminology to use. Regardless, I would still be interested to know if javascript has any way to remedy this issue. They've been around for a little while now and someone has to have found a workaround. –  David R. Sep 18 at 12:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.