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 know, I know--it isn't possible presently BUT my question is this:

Would it be possible to write a browser plug in for the major browsers that enables true, multi-threaded JavaScript?

It just seems like this would be possible and, if so, it would take the web (not to mention JavaScript) to new heights.

Thanks!

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

The DOM and it's various APIs are NOT thread-safe. No browser plug-in will be able to make them thread-safe without essentially breaking the pure multi-threading.

Some DOM functions like document.getElementsByTagName() are inherently thread-unsafe because they return a dynamic collection that automatically changes as the DOM is changed. You couldn't even safely loop through the results of that collection if some other thread might be changing the DOM at the same time. A new DOM API could be written with thread safety in mind, but that would be a major undertaking and some existing behaviors would have to be discontinued.

Plus, modern browsers already have web workers which allow pure javascript multi-threading (without DOM access in the threads) so you'd have to describe what more beyond web workers that doesn't require DOM access you are interested in implementing.

share|improve this answer
add comment

Brendan Eich has voiced his opposition to multi-threading in JavaScript several times:

So my default answer to questions such as the one I got at last May’s Ajax Experience, “When will you add threads to JavaScript?” is: “over your dead body!” (source)

And he goes on to list a few reasons why.

It's not just the DOM that's not thread-safe, the language itself does not lend itself to graceful multithreading. One of the big problems facing JavaScript is the highly flexible and dynamic type system. Since almost everything can be modified and redefined at run time, what happens to when the programmer modifies the body of Object.prototype.toString method while another thread is actually calling Object.prototype.toString? Or more generally, any method? The only safe way to handle this would be to synchronize any operations modifying the any method on any object -- since this a fairly common task in JavaScript, you would essentially revert it back to a single-threaded language again.

Web workers are able to get around this issue by ensuring that that the worker scripts do not share the same memory space as the main thread, so modifying objects in the work cannot interrupt or corrupt the operation of the main thread.

share|improve this answer
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.