- 论坛徽章:
- 1
|
好像MFC 中的SetTimer
我在网上看到一篇文章 ,但是好像代码没写完..它是怎么实现利用单线程实现,而且同时还可以处理其他任务的
- 最优雅的方法是使用RTC机制。利用select函数,你可以用单线程实现定时器,同时还可以处理其它任务。简单示例:
-
- #include <stdio.h>
- #include <linux/rtc.h>
- #include <sys/ioctl.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <errno.h>
-
- int main(int argc, char* argv[])
- {
- unsigned long i = 0;
- unsigned long data = 0;
- int retval = 0;
- int fd = open ("/dev/rtc", O_RDONLY);
-
- if(fd < 0)
- {
- perror("open");
- exit(errno);
- }
-
- /*Set the freq as 4Hz*/
- if(ioctl(fd, RTC_IRQP_SET, 4) < 0)
- {
- perror("ioctl(RTC_IRQP_SET)");
- close(fd);
- exit(errno);
- }
- /*Set the freq as 4Hz*/
- if(ioctl(fd, RTC_IRQP_SET, 4) < 0)
- {
- perror("ioctl(RTC_IRQP_SET)");
- close(fd);
- exit(errno);
- }
-
- /* Enable periodic interrupts */
- if(ioctl(fd, RTC_PIE_ON, 0) < 0)
- {
- perror("ioctl(RTC_PIE_ON)");
- close(fd);
- exit(errno);
- }
-
- for(i = 0; i < 100; i++)
- {
- if(read(fd, &data, sizeof(unsigned long)) < 0)
- {
- perror("read");
- close(fd);
- exit(errno);
- }
- printf("timer\n");
- }
- /* Disable periodic interrupts */
- ioctl(fd, RTC_PIE_OFF, 0);
- close(fd);
-
- return 0;
- }
复制代码 |
|