JavaScript的临界区或信号问题 [英] javascript critical sections or semaphore problem

查看:365
本文介绍了JavaScript的临界区或信号问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function myobj(){
  var gup=this;
  this.lastindex=-1;
  this.criticalSectionInTimer=0;
  this.updateTimer;

  this.start = function(l){
      if((typeof this.updateTimer)=="number"){
        clearInterval ( this.updateTimer );
      }
      this.updateTimer=setInterval(function() {gup.getMessages();} , 30);
    }

    this.stop= function(){
      if((typeof this.updateTimer)=="number"){
        clearInterval ( this.updateTimer );
      }  
    }

  this.addUpdate(i){
    //some code
  }

  this.rrrrnr=0;

  this.getMessages = function (){
    if(this.criticalSection==0){
       this.criticalSection=1;
      this.rrrrnr++;
      console.log("in critical section"+this.rrrrnr);
         var url="getmessages.php?lastindex="+this.lastindex;
         $.getJSON(url,
             function(data){
              gup.lastindex=data.lastindex;
              $.each(data.updates, function(i,item){
                gup.addUpdate(item);
              });
           }
         );
       console.log("out critical section"+this.rrrrnr);
       this.criticalSection=0;
    }
  }

}

var m= new myobj();
myobj.start();

我从上面的code。我有一个主循环,这使得在给定的时间间隔更新。问题是,我已经意识到越来越处于临界区这是我的变量this.criticalSection分隔。

I have the code from above. I have a main loop which makes updates at a given time interval. The problem is i have realized that it is getting in the "critical section" which I have delimited by the variable this.criticalSection .

从萤火我得到的消息,在临界区+指数和走出临界区+指数以正确的顺序,但Ajax请求仍在处理中。但我得到了相同的索引要求,我真的不知道到哪里寻找这个问题。

From firebug i get the messages "in critical section" + index and "out critical section" +index in the right order but the ajax request is still being processed. But I get request with the same index and i really don't know where to look for the problem.

有没有buildin功能在javascript信号灯或关键部分?

Are there any buildin features for semaphores or critical sections in javascript?

推荐答案

有没有信号灯和关键部分,因为JavaScript是单线程的。 Ajax调用你提出的是异步的,所以它揭开序幕的要求,然后愉快地保持下去,离开你的关键部分。正如其他人所说,一个简单的解决方案是使该请求同步的,但是这违背了阿贾克斯的目的。

There aren't semaphores or critical sections because JavaScript is single-threaded. The ajax call you make is asynchronous, so it kicks off the request and then happily keeps going and leaving your critical section. As others have mentioned, a simple solution is to make the request synchronous, but this defeats the purpose of ajax.

看你的code,好像你正在试图获得定期更新。如果是这样的话,为什么不安排在Ajax请求的回调的下一个更新?

Looking at your code, it seems like you are trying to get updates at regular intervals. If this is the case, why not schedule the next update in the callback of the ajax request?

this.getMessages = function (){
    var url="getmessages.php?lastindex="+this.lastindex;
    $.getJSON(url,
        function(data){
            gup.lastindex=data.lastindex;
            $.each(data.updates, function(i,item){
                gup.addUpdate(item);
            });
  gup.updateTimer=setTimeout(gup.getMessages, 30);
        }
    );

}

这将不再需要信号量,而更多的是与JavaScript的事件驱动性质线。不足之处是更新不是的确切的时间间隔进行。此外,30毫秒似乎是一个极短的时间间隔。

This would remove the need for semaphores, and is more in line with the event-driven nature of JavaScript. The downside is the updates are not done at exact intervals. Also, 30 milliseconds seems an extremely short interval.

这篇关于JavaScript的临界区或信号问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆