views:

358

answers:

7

hi, my problem is the following. I wrote a class AJAXEngine, which creates in the constructor a new XMLHttpRequest object. The class contains a method called responseAnalyser, which is called when the "onreadystatechange" of the XMLHttpRequest object has changed. So now I created lets say 4 instances of AJAXEngine => 4 XMLHttpRequest objects.

Now I have another class DataRequester, which has an array-attribute dataReq, which holds the intances of AJAXEngine. There is only one instance of DataReqeuster in the whole program! DataRequester has a function called callWhenFinished. The function is called, by the function responseAnalyser of AJAXEngine and decrements a variable of the DataRequester instance.

But, I think there happen race conditions. How could I prefent them in JavaScript?

function AJAXEngine
{
 this.httpReqObj = //create new XMLHttpRequest Object
 this.obj;
 this.func;
}
AJAXEngine.prototype.responseAnalyser = function()
{
 if(this.httpReqObj.readState == 4)
 {
  this.func.call(this.obj);
 }
}
AJAXEngine.prototype.fireReq = function(o, f)
{
 this.obj = o;
 this.func = f;
 // fire ajax req
}

function DataRequester()
{
 this.dataReq = new Array();
 this.test = 4;

 for(var i = 0; i < 4; i ++)
 {
  this.dataReq[i] = new AJAXEngine();
 }
}
DataRequester.prototype.callWhenFinished = function()
{
 this.test --;
}
A: 

First of all: most of AJAX-oriented browsers support convention "only 2 simultaneous requests to the same domain". So if you start 4 then 2 of them will be pended.

You DataReqeuster /singleton/ can have array of variable 'test', so instead of share single variable across multiple instances, create multiple instances of data. So to calculate result you will need to sum 'test' array.

Dewfy
A: 

Hi, sorry thats not an alternative. because this was just an example that should describe the problem. I also call another function, where I not just decrement one single attribute. so any other solutions?

Triptych
A: 

You would need to implement a makeshift mutex (the idea is that a heuristic would check for a bool and set it to true if it's false then do body, otherwise sleep(settimeout?) - this is obviously a pretty bad heuristic that nobody would implement as it is not thread safe, but that's the general concept of how you would deal with the race conditions anyway).

I believe there at least one example of creating a mutex on the web, but I have not looked over it in detail - it has some detractors, but I am unaware of another way to achieve 'thread safety' in javascript. I haven't ever needed to implement js 'thread-safety', but that's I start looking if I had to deal with race conditions in javascript.

laura
A: 

You can't do a mutex in javascript simply because there really is no built in sleep function available.

See: http://stackoverflow.com/questions/1277070/is-there-an-equivalent-javascript-or-jquery-sleep-function/1277202#1277202

Also, there is no way to ensure that the boolean flag in your mutex isn't being accessed at the same time as another thread, the boolean itself then needs a mutex... and so on and so on. You would need something like Synchronized keyword in java to be available in javascript and this simply doesn't exist. I have had situations where I was worried about thread safety, but when with the code anyway with an alternative plan if an error occurred but that has yet to happen.

So my advice, is if your getting an error, its probably not because of a race condition.

Zoidberg
A: 

what do you think about the following article? I just found it in google

http://www.developer.com/lang/jscript/article.php/3592016

+1  A: 

Not sure if this would help, but it looks like you're trying to create a managed connection pool. I did one a few years ago that still works fine here:

DP_RequestPool Library

The pool ensures that requests are made in the order you've provided them (although, of course, they may be returned in any order based on performance) using as many simultaneous requests as you define (subject to system limitations). You can instantiate multiple pools for different purposes.

If nothing else this might give you some ideas.

Jim Davis
can you give me the link where I can find the sources to have a look at it?
The link is in the answer - are you having a problem with it? Here it is with no markup:http://www.depressedpress.com/Content/Development/JavaScript/Extensions/DP_RequestPool/Index.cfmThe download link is in the first section of the page.
Jim Davis
hi, yes I have a problem with it. Every time I click on the link, I get a blue box with the title "Error Occurred While Processing Request" Element DP is undefined in a Java object of type class [Ljava.lang.String; referenced as
Sorry - dumb bug on my side (I just moved my site to a new server/version of ColdFusion, but failed to change a pointer for new users). It should work now.The links above should work to reach the documentation, but the link to the code specifically is:http://www.depressedpress.com/Content/Development/JavaScript/Extensions/DP_RequestPool/Archives/DP_RequestPool.zipSorry about the frustration agian, but thanks for the help in pointing out my stupidity!
Jim Davis
thanks, now it works; and no problem ;-)
A: 

hi yes that is exactly what I want to have. how can I have a look at the source??

You should answer to other people by using the 'add comment' link under their answers.
Robert Munteanu