Is JavaScript guaranteed to be single threaded?

R

Ranju. V

Hi,

I use a lot asynchronous XMLHTTP from JavaScript. Are the callbacks
that we register with XMLHTTP guaranteed to execute single-threaded?
For instance, consider the following code snippet.

var xmlhttp1 = new ActiveXObject( "Msxml2.XMLHTTP.4.0" );
xmlhttp1.onreadystatechange = callback;
xmlhttp1.open( "POST", "foo.aspx", true );
xmlhttp1.send( data1 );

var xmlhttp2 = new ActiveXObject( "Msxml2.XMLHTTP.4.0" );
xmlhttp2.onreadystatechange = callback;
xmlhttp2.open( "POST", "foo.aspx", true );
xmlhttp2.send( data2 );

function callback()
{
//
// will two instances of this function run
// parallely?
//
}

Will the function "callback" be run parallelly should both the XMLHTTP
requests return simultaneously? Or does IE somehow serialize calls to
the callback routine? Its important for me that the callback executes
single threaded since things like critical sections aren't possible in
JavaScript.

Thanks.
 
J

John G Harris

Hi,

I use a lot asynchronous XMLHTTP from JavaScript. Are the callbacks
that we register with XMLHTTP guaranteed to execute single-threaded?
<snip>

Guaranteed. Software.

You don't often see those two words in the same sentence.

John
 
R

Ranju. V

<snip>

Guaranteed. Software.
You don't often see those two words in the same sentence.

Thanks. That's a relief. And yes, those two words aren't really the
best of pals!
 
J

John G Harris

Thanks. That's a relief. And yes, those two words aren't really the
best of pals!

I'm afraid I said it in a misleading way. I'm suggesting that software
companies seldom make guarantees. Even when they do they aren't always
telling the truth :-(

John
 
J

Jim Ley

I use a lot asynchronous XMLHTTP from JavaScript. Are the callbacks
that we register with XMLHTTP guaranteed to execute single-threaded?
For instance, consider the following code snippet.

Yes, they are single threaded in all implementations.

(of course in your example there's no guarantee that the callback of
the xmlhttp1 be called before the callback of xmlhttp2, but they will
not be called simultaneously.)

Jim.
 
R

Ranju. V

I'm afraid I said it in a misleading way. I'm suggesting that
software
companies seldom make guarantees. Even when they do they aren't always
telling the truth :-(

True enough. I mean does anybody ever read the license agreements that
software installation programs make the user agree to? If you spend a
little time and actually read one of those agreements then you'll find
that they pretty much devolve themselves of all responsibility should
something go wrong!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top