Timmer

Linux下定时器的使用

倖福魔咒の 提交于 2019-11-27 08:02:01
Linux下应用层定时器本来有好几种,大伙可以去搜索其他帖子博客,这里我主要描述我在使用setitimer时候遇到的问题,话不多说,直接上代码吧 例子一:只有定时器任务,为模拟复杂,我特意加个锁操作 // lock_timmer_test.cpp #include <iostream> #include <sys/time.h> #include <signal.h> #include <linux/types.h> #include <sched.h> #include <pthread.h> using namespace std; //互斥锁 class MutexLock { public: MutexLock(){ pthread_mutex_init(&m_stMutex, NULL); } ~MutexLock(){ pthread_mutex_destroy(&m_stMutex); } void lock(){ pthread_mutex_lock(&m_stMutex);} int unlock() {return pthread_mutex_unlock(&m_stMutex); } bool trylock(){ return pthread_mutex_trylock(&m_stMutex) == 0;} pthread_mutex_t*