##线程停止
//线程停止应用
public: explicit WorkerThread(QObject *parent = 0) :
QThread(parent), m_bStopped(false)
{
qDebug() << "Worker Thread : " << QThread::currentThreadId();
}
~WorkerThread()
{
stop();
quit();
wait();
}
void stop() {
qDebug() << "Worker Stop Thread : " << QThread::currentThreadId();
QMutexLocker locker(&m_mutex);
m_bStopped = true;
}
高精度定时器 |
在 Qt 中,实现高精度计时。该类提供了纳秒级别的计时精度,可以使用其clock_nanosleep()函数来获取从启动计时器开始到当前时间的纳秒数。
以下是一个简单的示例:
#define FREQUENCY 1000
#define CLOCK_TO_USE CLOCK_REALTIME
#define NSEC_PER_SEC (1000000000L)
#define PERIOD_NS (NSEC_PER_SEC / FREQUENCY ) /*本次设置周期PERIOD_NS为1ms*/
const struct timespec cycletime = {0, PERIOD_NS};
void *thread(void * arg)
{
struct timespec wakeupTime, time;
clock_gettime(CLOCK_TO_USE, &wakeupTime);
while (1)
{
wakeupTime = timespec_add(wakeupTime, cycletime);
clock_nanosleep(CLOCK_TO_USE, TIMER_ABSTIME, &wakeupTime, NULL);
}
}
【关注微信公众号一起来交流】 |