代码之家  ›  专栏  ›  技术社区  ›  perilbrain

优化手表代码

  •  1
  • perilbrain  · 技术社区  · 16 年前

    我想实现一个代码来监视假设的某个事件…同时我没有任何内置的事件监视程序,所以我必须实现我的一个..它消耗的CPU和内存最少。

    你能给我推荐一个..

    例如,给出了一个伪代码:

    while(true)
    {
        if(process.isrunning)
            process.kill();
    }
    
    3 回复  |  直到 16 年前
        1
  •  1
  •   Arve    16 年前

    如果没有任何事件可连接,则代码必须处于“活动”状态才能运行检查。这会消耗CPU周期。

    可以减轻浪费的方法是在睡眠中添加一个调用(THEAD.O.NET中的睡眠,在C++的一些实现中睡眠)。

    while (true) {
        if(process.isrunning)
             process.kill();
    
        sleep(100);   // Wait 100 millisecond before trying again 
    }
    

    但这会让你的代码响应性降低一点。

        2
  •  0
  •   uray    16 年前

    您可以尝试使用计时器队列: http://msdn.microsoft.com/en-us/library/ms687003%28VS.85%29.aspx 它基本上使用内核调度程序以指定的间隔调用函数回调,调用方来自不同的线程,因此它不会中断主线程并使应用程序响应,该线程由Windows管理,因此您不必管理自己的池线程,并且相对准确。

    实施示例: `

    //a singleton class that hold timer queue
    class TimerQueue {
        protected:
            HANDLE timerQueue;
            TimerQueue() { 
                this->timerQueue = ::CreateTimerQueue(); 
            }
            ~TimerQueue() {
                if(this->timerQueue) {
                    ::DeleteTimerQueueEx(this->timerQueue,NULL);
                    this->timerQueue = NULL;
                }
            }
        public:     
            static HANDLE getHandle() {
                static TimerQueue timerQueueSingleton;
                return timerQueueSingleton.timerQueue;
            }
    }
    
    //timer base class
    class Timer
    {
    protected:
        HANDLE timer;
        virtual void timerProc() = 0;
        static void CALLBACK timerCallback(PVOID param,BOOLEAN timerOrWait) {
            Timer* self = (Timer*)param;
            self->timerProc();
        }
    public:
        Timer(DWORD startTimeMs,DWORD periodTimeMs) {       
            if(!::CreateTimerQueueTimer( &this->timer, TimerQueue::getHandle(), 
                                        (WAITORTIMERCALLBACK)&this->timerCallback, 
                                         this, startTimeMs, periodTimeMs,
                                         WT_EXECUTEDEFAULT) ) {
                this->timer = NULL;
            }
        }
        virtual ~Timer() {
            if(this->timer) {
                ::DeleteTimerQueueTimer(TimerQueue::getHandle(),&this->timer,NULL);
                this->timer = NULL;
            }
        }
    }
    
    //derive and implement timerProc
    class MyTimer : public Timer
    {
        protected:
            virtual void timerProc() {
                if(process.isRunning()) {
                    process.kill();
                }
            }
        public:
            MyTimer(DWORD startTimeMs,DWORD periodTimeMs) 
                : Timer(startTimeMs,periodTimeMs) {}
    }
    
    //usage:
    int main(int argc,char* argv[]) {
        MyTimer timer(0,100); //start immediately, at 10 Hz interval
    }
    

    `

    免责声明:我不测试或编译这些代码,你应该重新检查它

        3
  •  0
  •   Tim Palak Chaudhary    16 年前

    尽管您已经将其标记为语言不可知的,但是任何好的实现都会有很大的不同,不仅是在不同的语言之间,而且在不同的操作系统之间。在很多情况下,程序或操作系统功能只需要做这类事情,并且已经实现了一些机制,以尽可能合理、无干扰的方式来做这件事。

    如果您有一种特定的语言和/或操作系统,请告诉我们,并给我们一个更好的想法,你正在努力实现。这样,我们就可以把你引向这个问题许多可能的解决办法中最适当的一个。

    推荐文章