Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 互斥锁等待时间和看门狗_C++ - Fatal编程技术网

C++ 互斥锁等待时间和看门狗

C++ 互斥锁等待时间和看门狗,c++,C++,我们正在衡量多线程实时应用程序的性能。我需要编写一个适配器来测量在上花费的时间 锁定互斥锁的等待时间 mutex.lock->mutex.unlock之间的时间:关键部分执行时间 以及锁定时器或看门狗定时器等组件。如果线程持有互斥锁的时间超过配置的时间。。必须通知错误日志 有什么最好的方法吗?在调用互斥锁之前和获得互斥锁之后,您可以花点时间(使用标准的ctime)。这两者之间的差异将为您提供线程等待获取互斥的大致时间。 可以对过程2执行类似的过程,以确定关键段的执行时间 也许RAII这个成语对你

我们正在衡量多线程实时应用程序的性能。我需要编写一个适配器来测量在上花费的时间

  • 锁定互斥锁的等待时间
  • mutex.lock->mutex.unlock之间的时间:关键部分执行时间
  • 以及锁定时器或看门狗定时器等组件。如果线程持有互斥锁的时间超过配置的时间。。必须通知错误日志

    有什么最好的方法吗?

    在调用互斥锁之前和获得互斥锁之后,您可以花点时间(使用标准的ctime)。这两者之间的差异将为您提供线程等待获取互斥的大致时间。

    可以对过程2执行类似的过程,以确定关键段的执行时间

    也许RAII这个成语对你有帮助。例如:

    class MutexHolder
    {
    public:
        MutexHolder()
        {
            //here you should take start time
            m_mutex.lock();
            //here you should take time and find a differnce between start time 
        }
        ~MutexHolder()
        {
            m_mutex.unlock();
            //here you should track the time spent between mutex.lock -> mutex.unlock and do smth else
        }
    private:
        Mutex m_mutex;
    };
    
    struct WatchDog
    {
        int n, limit, start;
    
        WatchDog(int n, int limit) : n(n), limit(limit)
        {
            start = timer();
        }
    
        ~WatchDog()
        {
            int delta = timer() - start;
            if (delta > limit)
            {
                log("WatchDog(%i): Time limit exceeded (%i > %i)",
                    n, delta, limit);
            }
            timecounters[n] += delta;
        }
    };
    
    然后使用该类:

    //your code
    
    {//start of critical section
        MutexHolder lock;
    
        //code guarded by mutex locking
    
    }//here the destructor of the MutexHolder object will call automatically
    

    你可以做的一件简单的事情就是使用统计计数器。首先定义需要多少计数器。。。说10

    int timecounters[10];
    
    然后使用你所有的计时器。。。更细的粒度和更低的开销当然是最好的。。。例如clock()/GetTickCount()/QueryPerformanceCounter/rdtsc。 最后使用一个类似下面的stopwatch类

    struct StopWatch
    {
        int n;
        StopWatch(int n) : n(n) { timecounters[n] -= timer(); }
        ~StopWatch() { timecounters[n] += timer(); }
    };
    
    然后,对于需要跟踪写入的每一段代码

    {
        StopWatch sw(1);
        // code to be instrumented
    }
    
    在程序执行结束时,您将拥有在各种插入指令的部分中花费的总时间,并且开销应该相当低。对单个执行时间添加限制检查也很容易。。。例如:

    class MutexHolder
    {
    public:
        MutexHolder()
        {
            //here you should take start time
            m_mutex.lock();
            //here you should take time and find a differnce between start time 
        }
        ~MutexHolder()
        {
            m_mutex.unlock();
            //here you should track the time spent between mutex.lock -> mutex.unlock and do smth else
        }
    private:
        Mutex m_mutex;
    };
    
    struct WatchDog
    {
        int n, limit, start;
    
        WatchDog(int n, int limit) : n(n), limit(limit)
        {
            start = timer();
        }
    
        ~WatchDog()
        {
            int delta = timer() - start;
            if (delta > limit)
            {
                log("WatchDog(%i): Time limit exceeded (%i > %i)",
                    n, delta, limit);
            }
            timecounters[n] += delta;
        }
    };
    

    当然,
    看门狗
    类永远不会中断一个活动,如果它花费的时间超过了应该的时间。。。它只会在活动结束时报告问题。真正的中断通用看门狗类的实现要复杂得多。

    它是用于Windows的吗?您正在使用WaitForSingleObject()?正如Jaywalker指出的,这个问题与操作系统有很大关系,所以您应该指定它。@Jaywalker&Simone-Its在Solaris平台上。Sun Sparc 32G内存,8核。它有很多上下文切换。。。