0

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 --;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

4 Answers4

1

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
  • 1,230
  • 6
  • 11
  • can you give me the link where I can find the sources to have a look at it? –  Aug 20 '09 at 05:08
  • 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.cfm The download link is in the first section of the page. – Jim Davis Aug 20 '09 at 05:17
  • 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 –  Aug 20 '09 at 09:10
  • 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.zip Sorry about the frustration agian, but thanks for the help in pointing out my stupidity! – Jim Davis Aug 20 '09 at 14:24
0

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
  • 23,277
  • 13
  • 73
  • 121
0

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
  • 7,280
  • 4
  • 35
  • 43
0

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.

Community
  • 1
  • 1
Zoidberg
  • 10,137
  • 2
  • 31
  • 53