如何在c++代码块中使用定时器
How to use timer in c++ CodeBlock
K,大家好,我正在试着做这样的东西:"写一个程序,在电脑屏幕上显示还有多少时间上课结束:如果在课程结束前还有超过30分钟的时间要打印报告……",于是我尝试了:
#include <iostream>
#include <cstdio>
#include <ctime>
using namespace std;
int main() {
int startTime = 1, endTime;
endTime = 30 * 3600;
clock_t start = clock();
cout<<"Time: "<< start <<'n';
if (start >= 7200)
{
// do something
} else if (start == endTime)
{
// do something
}
}
我想让它显示时间,如果时间==数字,然后做一些事情。试着睡();但是由于某些原因,我在codeBlock中出现了错误。
我得到"sleep未在此作用域中声明"。
如果是Unix系统,则为#include <unistd.h>
.
如果Windows使用#include <windows.h>
,则使用Sleep()
。
有很多方法可以做到。以下是其中的两个:
使用boost timer库是更好的方法:
http://www.boost.org/doc/libs/1_53_0/libs/timer/doc/original_timer.html
或者启动一个线程
static const int INTERVAL_SEC = 60;
static void* thread_timer_process(void*)
{
do
{
// Do something;
sleep(INTERVAL_SEC);
// don't forget to put a cancel point in here
} while(true);
}
int main()
{
pthread_t thread_id;
thread_create(&thread_id, null, thread_timer_process, NULL);
// Other things
pthread_cancel(thread_id); // interrupt thread running.
void *result = NULL;
pthread_join(thread_id, &result); // wait for thread ending.
}
相关文章:
- Ardunio UNO解决了多个重叠的定时器循环
- 我们能否在stm32f中使用硬件定时器控制两个独立的进程
- Arduino Nano:A4988 使用串行输入时通过定时器进行步进控制不稳定
- 没有信号处理程序的POSIX定时器的目的是什么?
- 在定时器回调函数中使用 Sleep() 会导致C++出现问题吗?
- 在 Metal 着色器代码中,如何定义函数的 in/out 参数变量?
- 如何在Qt中修改QWebsocket定时器
- 如何将 MATLAB 图像处理库内置函数转换为 MATLAB 编码器代码生成不支持的 C++?
- 如何使用 gcc 内联汇编器代码访问成员变量
- 为什么此串行/调制解调器代码弄乱了我的终端显示
- VS2015 中的方法存在检查器代码中断
- C++Windows中的高分辨率定时器库
- 如何用单个信号处理程序解决这个多linux定时器的问题
- C#/.NET定时器和Win32睡眠函数都是不精确的
- Udp 在 QT 定时器信号中写入数据报
- 如何避免编写样板访问器代码
- Qt 创建器 - 代码正在运行,但我从 IDE 收到错误消息
- OpenGL中的着色器代码不正确.我想念什么
- 此迭代器代码中发生了什么?
- 如何在c++代码块中使用定时器