Question

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 instances 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 --;
}
Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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

See: Is there an equivalent Javascript or Jquery sleep function?

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.

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top