iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >利用C语言实现经典多级时间轮定时器
  • 573
分享到

利用C语言实现经典多级时间轮定时器

2024-04-02 19:04:59 573人浏览 安东尼
摘要

目录1. 序言 2. 多级时间轮实现框架2.1 多级时间轮对象2.2 时间轮对象2.3 定时任务对象2.4 双向链表 2.5 联结方式 3. 多级时间轮C语言实现 3.1 双向链表头

1. 序言

最近一直在找时间轮的C语言实现代码,发现很多都是Java或者c++实现的。而我对其他语言不熟悉,看不太懂。关于C实现的,让我如沐春风的实现没找到,GitHub上也只找打一个135星的项目,它的具体实现还没来得及看。后来经过多方搜索,找到了两个比较类似的代码,博主都称参考linux源码中的实现,但是我没有找到对应的代码,个人感觉他们代码实现的很好,经过整理后再次分享出来供以后学习(反正我自己写不出来,我尝试写了一个简单时间轮的代码,是在不敢直视)。

2. 多级时间轮实现框架

上图是5个时间轮级联的效果图。中间的大轮是工作轮,只有在它上的任务才会被执行;其他轮上的任务时间到后迁移到下一级轮上,他们最终都会迁移到工作轮上而被调度执行。

多级时间轮的原理也容易理解:就拿时钟做说明,秒针转动一圈分针转动一格;分针转动一圈时针转动一格;同理时间轮也是如此:当低级轮转动一圈时,高一级轮转动一格,同时会将高一级轮上的任务重新分配到低级轮上。从而实现了多级轮级联的效果。

2.1 多级时间轮对象

多级时间轮应该至少包括以下内容:

  • 每一级时间轮对象
  • 轮子上指针的位置

关于轮子上指针的位置有一个比较巧妙的办法:那就是位运算。比如定义一个无符号整型的数:

==通过获取当前的系统时间便可以通过位操作转换为时间轮上的时间,通过与实际时间轮上的时间作比较,从而确定时间轮要前进调度的时间,进而操作对应时间轮槽位对应的任务==。

为什么至少需要这两个成员呢?

  • 定义多级时间轮,首先需要明确的便是级联的层数,也就是说需要确定有几个时间轮。
  • 轮子上指针位置,就是当前时间轮运行到的位置,它与真实时间的差便是后续时间轮需要调度执行,它们的差值是时间轮运作起来的驱动力。

多级时间轮对象的定义


//实现5级时间轮 范围为0~ (2^8 * 2^6 * 2^6 * 2^6 *2^6)=2^32
struct tvec_base
{
    unsigned long 		current_index;   
    pthread_t  			thincrejiffies;
    pthread_t  			threadID;
    struct tvec_root 	tv1;	
    struct tvec      	tv2;	
    struct tvec      	tv3;	
    struct tvec      	tv4;	
    struct tvec      	tv5;	
};

2.2 时间轮对象

我们知道每一个轮子实际上都是一个哈希表,上面我们只是实例化了五个轮子的对象,但是五个轮子具体包含什么,有几个槽位等等没有明确(即struct tvec和struct tvec_root)。


#define TVN_BITS 		6
#define TVR_BITS 		8
#define TVN_SIZE 		(1<<TVN_BITS)
#define TVR_SIZE 		(1<<TVR_BITS)

struct tvec {
    struct list_head vec[TVN_SIZE];
};
 
struct tvec_root{
    struct list_head vec[TVR_SIZE];
};

此外,每一个时间轮都是哈希表,因此它的类型应该至少包含两个指针域来实现双向链表的功能。这里我们为了方便使用通用的struct list_head的双向链表结构。

2.3 定时任务对象

定时器的主要工作是为了在未来的特定时间完成某项任务,而这个任务经常包含以下内容:

  • 任务的处理逻辑(回调函数)
  • 任务的参数
  • 双向链表节点
  • 到时时间

定时任务对象的定义


typedef void (*timeouthandle)(unsigned long );
 
struct timer_list{
    struct list_head entry;          //将时间连接成链表
    unsigned long expires;           //超时时间
    void (*function)(unsigned long); //超时后的处理函数
    unsigned long data;              //处理函数的参数
    struct tvec_base *base;          //指向时间轮
};

在时间轮上的效果图:

2.4 双向链表

在时间轮上我们采用双向链表的数据类型。采用双向链表的除了操作上比单链表复杂,多占一个指针域外没有其他不可接收的问题。而多占一个指针域在今天大内存的时代明显不是什么问题。至于双向链表操作的复杂性,我们可以通过使用通用的struct list结构来解决,因为双向链表有众多的标准操作函数,我们可以通过直接引用list.h头文件来使用他们提供的接口。

struct list可以说是一个万能的双向链表操作框架,我们只需要在自定义的结构中定义一个struct list对象即可使用它的标准操作接口。同时它还提供了一个类似container_of的接口,在应用层一般叫做list_entry,因此我们可以很方便的通过struct list成员找到自定义的结构体的起始地址。

关于应用层的log.h, 我将在下面的代码中附上该文件。如果需要内核层的实现,可以直接从linux源码中获取。

2.5 联结方式

多级时间轮效果图:

3. 多级时间轮C语言实现

3.1 双向链表头文件: list.h

提到双向链表,很多的源码工程中都会实现一系列的统一的双向链表操作函数。它们为双向链表封装了统计的接口,使用者只需要在自定义的结构中添加一个struct list_head结构,然后调用它们提供的接口,便可以完成双向链表的所有操作。这些操作一般都在list.h的头文件中实现。Linux源码中也有实现(内核态的实现)。他们实现的方式基本完全一样,只是实现的接口数量和功能上稍有差别。可以说这个==list.h文件是学习操作双向链表的不二选择==,它几乎实现了所有的操作:增、删、改、查、遍历、替换、清空等等。这里我拼凑了一个源码中的log.h函数,终于凑够了多级时间轮中使用到的接口(原来的博主没有提供list.h文件,只能自己去东拼西凑)。


#if !defined(_BLKID_LIST_H) && !defined(LIST_HEAD)
#define _BLKID_LIST_H

#ifdef __cplusplus 
extern "C" {
#endif



struct list_head {
	struct list_head *next, *prev;
};

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
	struct list_head name = LIST_HEAD_INIT(name)

#define INIT_LIST_HEAD(ptr) do { \
	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)

static inline void
__list_add(struct list_head *entry,
                struct list_head *prev, struct list_head *next)
{
    next->prev = entry;
    entry->next = next;
    entry->prev = prev;
    prev->next = entry;
}


static inline void
list_add(struct list_head *entry, struct list_head *head)
{
    __list_add(entry, head, head->next);
}


static inline void
list_add_tail(struct list_head *entry, struct list_head *head)
{
    __list_add(entry, head->prev, head);
}

static inline void
__list_del(struct list_head *prev, struct list_head *next)
{
    next->prev = prev;
    prev->next = next;
}


static inline void
list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
}

static inline void
list_del_init(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    INIT_LIST_HEAD(entry);
}

static inline void list_move_tail(struct list_head *list,
				  struct list_head *head)
{
	__list_del(list->prev, list->next);
	list_add_tail(list, head);
}


static inline int
list_empty(struct list_head *head)
{
    return head->next == head;
}



static inline void list_replace(struct list_head *old,
				struct list_head *new)
{
	new->next = old->next;
	new->next->prev = new;
	new->prev = old->prev;
	new->prev->next = new;
}


#define list_first_entry(ptr, type, member) \
    list_entry((ptr)->next, type, member)

static inline void list_replace_init(struct list_head *old,
					struct list_head *new)
{
	list_replace(old, new);
	INIT_LIST_HEAD(old);
}


#define list_entry(ptr, type, member) \
	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))


#define list_for_each(pos, head) \
	for (pos = (head)->next; pos != (head); pos = pos->next)


#define list_for_each_safe(pos, pnext, head) \
	for (pos = (head)->next, pnext = pos->next; pos != (head); \
	     pos = pnext, pnext = pos->next)

#ifdef __cplusplus
}
#endif

#endif 

这里面一般会用到一个重要实现:==container_of==, 它的原理如果不清楚的话,可以阅读另一篇专门介绍该函数的博文:container of()函数简介

3.2 调试信息头文件: log.h

这个头文件实际上不是必须的,我只是用它来添加调试信息(代码中的errlog(), log()都是log.h中的宏函数)。它的效果是给打印的信息加上颜色,效果如下:

log.h的代码如下:


#ifndef _LOG_h_
#define _LOG_h_
#include <stdio.h>

#define COL(x)  "\033[;" #x "m"
#define RED     COL(31)
#define GREEN   COL(32)
#define YELLOW  COL(33)
#define BLUE    COL(34)
#define MAGENTA COL(35)
#define CYAN    COL(36)
#define WHITE   COL(0)
#define GRAY    "\033[0m"

#define errlog(fmt, arg...) do{     \
    printf(RED"[#ERROR: Toeny Sun:"GRAY YELLOW" %s:%d]:"GRAY WHITE fmt GRAY, __func__, __LINE__, ##arg);\
}while(0)

#define log(fmt, arg...) do{     \
    printf(WHITE"[#DEBUG: Toeny Sun: "GRAY YELLOW"%s:%d]:"GRAY WHITE fmt GRAY, __func__, __LINE__, ##arg);\
}while(0)

#endif

3.3 时间轮代码: timewheel.c



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include "list.h"
#include "log.h" 

#define TVN_BITS 		6
#define TVR_BITS 		8
#define TVN_SIZE 		(1<<TVN_BITS)
#define TVR_SIZE 		(1<<TVR_BITS)
		
#define TVN_MASK 		(TVN_SIZE - 1)
#define TVR_MASK 		(TVR_SIZE - 1) 
 
#define SEC_VALUE 		0
#define USEC_VALUE 		2000
 
struct tvec_base;

#define INDEX(N) ((ba->current_index >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
 
typedef void (*timeouthandle)(unsigned long );
 
 
struct timer_list{
    struct list_head entry;          //将时间连接成链表
    unsigned long expires;           //超时时间
    void (*function)(unsigned long); //超时后的处理函数
    unsigned long data;              //处理函数的参数
    struct tvec_base *base;          //指向时间轮
};
 
struct tvec {
    struct list_head vec[TVN_SIZE];
};
 
struct tvec_root{
    struct list_head vec[TVR_SIZE];
};
 
//实现5级时间轮 范围为0~ (2^8 * 2^6 * 2^6 * 2^6 *2^6)=2^32
struct tvec_base
{
    unsigned long 		current_index;   
    pthread_t  			thincrejiffies;
    pthread_t  			threadID;
    struct tvec_root 	tv1;	
    struct tvec      	tv2;	
    struct tvec      	tv3;	
    struct tvec      	tv4;	
    struct tvec      	tv5;	
};
 
static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
{
    struct list_head *vec;
    unsigned long expires = timer->expires;	
    unsigned long idx = expires - base->current_index;

#if 1 
    if( (signed long)idx < 0 ) 
    {
        vec = base->tv1.vec + (base->current_index & TVR_MASK);
    }
	else if ( idx < TVR_SIZE ) 
    {
        int i = expires & TVR_MASK;
        vec = base->tv1.vec + i;
    }
    else if( idx < 1 << (TVR_BITS + TVN_BITS) )
    {
        int i = (expires >> TVR_BITS) & TVN_MASK;
        vec = base->tv2.vec + i;
    }
    else if( idx < 1 << (TVR_BITS + 2 * TVN_BITS) )
    {
        int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
        vec = base->tv3.vec + i;
    }
    else if( idx < 1 << (TVR_BITS + 3 * TVN_BITS) )
    {
        int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
        vec = base->tv4.vec + i;
    }
    else											 
    {
        int i;
        if (idx > 0xffffffffUL) 
        {
            idx = 0xffffffffUL;
            expires = idx + base->current_index;
        }
        i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
        vec = base->tv5.vec + i;
    }
#else
	;
#endif 
    list_add_tail(&timer->entry, vec);
}
 
static inline void detach_timer(struct timer_list *timer)
{
    struct list_head *entry = &timer->entry;
    __list_del(entry->prev, entry->next);
    entry->next = NULL;
    entry->prev = NULL;
}
 
static int __mod_timer(struct timer_list *timer, unsigned long expires)
{        
    if(NULL != timer->entry.next)
        detach_timer(timer);
	
    internal_add_timer(timer->base, timer); 
 
    return 0;
}
 
//修改定时器的超时时间外部接口
int mod_timer(void *ptimer, unsigned long expires)
{
    struct timer_list *timer  = (struct timer_list *)ptimer;
    struct tvec_base *base;
	 
	base = timer->base;
    if(NULL == base)
        return -1;
    
    expires = expires + base->current_index; 	
    if(timer->entry.next != NULL  && timer->expires == expires)
        return 0;
 
    if( NULL == timer->function )
    {
        errlog("timer's timeout function is null\n");
        return -1;
    }
	
	timer->expires = expires;
    return __mod_timer(timer,expires);
}
 
//添加一个定时器
static void __ti_add_timer(struct timer_list *timer)
{
    if( NULL != timer->entry.next )
    {
        errlog("timer is already exist\n");
        return;
    }
 
    mod_timer(timer, timer->expires);            
}
 

void* ti_add_timer(void *ptimewheel, unsigned long expires,timeouthandle phandle, unsigned long arg)
{
    struct timer_list  *ptimer;
 
    ptimer = (struct timer_list *)malloc( sizeof(struct timer_list) );
    if(NULL == ptimer)
        return NULL;
 
    bzero( ptimer,sizeof(struct timer_list) );        
    ptimer->entry.next = NULL;
    ptimer->base = (struct tvec_base *)ptimewheel; 
    ptimer->expires = expires;
    ptimer->function  = phandle;
    ptimer->data = arg;
 
    __ti_add_timer(ptimer);
 
    return ptimer;
}
 

void ti_del_timer(void *p)
{
    struct timer_list *ptimer =(struct timer_list*)p;
 
    if(NULL == ptimer)
        return;
 
    if(NULL != ptimer->entry.next)
        detach_timer(ptimer);
    
    free(ptimer);
}
 
static int cascade(struct tvec_base *base, struct tvec *tv, int index)
{
    struct list_head *pos,*tmp;
    struct timer_list *timer;
    struct list_head tv_list;
    
	
    list_replace_init(tv->vec + index, &tv_list);
 
    list_for_each_safe(pos, tmp, &tv_list)
    {
        timer = list_entry(pos,struct timer_list,entry);
        internal_add_timer(base, timer);
    }
 
    return index;
}
 
static void *deal_function_timeout(void *base)
{
    struct timer_list *timer;
    int ret;
    struct timeval tv;
    struct tvec_base *ba = (struct tvec_base *)base;
    
    for(;;)
    {
        gettimeofday(&tv, NULL);  
        while( ba->current_index <= (tv.tv_sec*1000 + tv.tv_usec/1000) )
        {         
           struct list_head work_list;
           int index = ba->current_index & TVR_MASK;
           struct list_head *head = &work_list;
		   
           if(!index && (!cascade(ba, &ba->tv2, INDEX(0))) &&( !cascade(ba, &ba->tv3, INDEX(1))) && (!cascade(ba, &ba->tv4, INDEX(2))) )
               cascade(ba, &ba->tv5, INDEX(3));
           
            ba->current_index ++;
            list_replace_init(ba->tv1.vec + index, &work_list);
            while(!list_empty(head))
            {
                void (*fn)(unsigned long);
                unsigned long data;
                timer = list_first_entry(head, struct timer_list, entry);
                fn = timer->function;
                data = timer->data;
                detach_timer(timer);
                (*fn)(data);  
            }
        }
    }
}
 
static void init_tvr_list(struct tvec_root * tvr)
{
    int i;
 
    for( i = 0; i<TVR_SIZE; i++ )
        INIT_LIST_HEAD(&tvr->vec[i]);
}
 
 
static void init_tvn_list(struct tvec * tvn)
{
    int i;
 
    for( i = 0; i<TVN_SIZE; i++ )
        INIT_LIST_HEAD(&tvn->vec[i]);
}
 
//创建时间轮  外部接口
void *ti_timewheel_create(void )
{
    struct tvec_base *base;
    int ret = 0;
    struct timeval tv;
 
    base = (struct tvec_base *) malloc( sizeof(struct tvec_base) );
    if( NULL==base )
        return NULL;
    
    bzero( base,sizeof(struct tvec_base) );
        
    init_tvr_list(&base->tv1);
    init_tvn_list(&base->tv2);
    init_tvn_list(&base->tv3);
    init_tvn_list(&base->tv4);
    init_tvn_list(&base->tv5);
    
    gettimeofday(&tv, NULL);
    base->current_index = tv.tv_sec*1000 + tv.tv_usec/1000;
 
    if( 0 != pthread_create(&base->threadID,NULL,deal_function_timeout,base) )
    {
        free(base);
        return NULL;
    }    
    return base;
}
 
static void ti_release_tvr(struct tvec_root *pvr)
{
    int i;
    struct list_head *pos,*tmp;
    struct timer_list *pen;
 
    for(i = 0; i < TVR_SIZE; i++)
    {
        list_for_each_safe(pos,tmp,&pvr->vec[i])
        {
            pen = list_entry(pos,struct timer_list, entry);
            list_del(pos);
            free(pen);
        }
    }
}
 
static void ti_release_tvn(struct tvec *pvn)
{
    int i;
    struct list_head *pos,*tmp;
    struct timer_list *pen;
 
    for(i = 0; i < TVN_SIZE; i++)
    {
        list_for_each_safe(pos,tmp,&pvn->vec[i])
        {
            pen = list_entry(pos,struct timer_list, entry);
            list_del(pos);
            free(pen);
        }
    }
}
 
 

void ti_timewheel_release(void * pwheel)
{  
    struct tvec_base *base = (struct tvec_base *)pwheel;
    
    if(NULL == base)
        return;
 
    ti_release_tvr(&base->tv1);
    ti_release_tvn(&base->tv2);
    ti_release_tvn(&base->tv3);
    ti_release_tvn(&base->tv4);
    ti_release_tvn(&base->tv5);
 
    free(pwheel);
}
 

struct request_para{
    void *timer;
    int val;
};
 
void mytimer(unsigned long arg)
{
    struct request_para *para = (struct request_para *)arg;
 
    log("%d\n",para->val);
    mod_timer(para->timer,3000);  //进行再次启动定时器
 
	sleep(10);
 
    //定时器资源的释放是在这里完成的
    //ti_del_timer(para->timer);
}
 
int main(int arGC,char *argv[])
{
    void *pwheel = NULL;
    void *timer  = NULL;
    struct request_para *para;
   
  
    para = (struct request_para *)malloc( sizeof(struct request_para) );
    if(NULL == para)
        return 0;
    bzero(para,sizeof(struct request_para));
 
    //创建一个时间轮
    pwheel = ti_timewheel_create();
    if(NULL == pwheel)
        return -1;
   
    //添加一个定时器
    para->val = 100;
    para->timer = ti_add_timer(pwheel, 3000, &mytimer, (unsigned long)para);
    
    while(1)
    {
        sleep(2);
    }
 
    //释放时间轮
    ti_timewheel_release(pwheel);
    
    return 0;
}

3.4 编译运行


toney@ubantu:/mnt/hgfs/em嵌入式学习记录/4. timerwheel/2. 多级时间轮$ ls
a.out  list.h  log.h  mutiTimeWheel.c
toney@ubantu:/mnt/hgfs/em嵌入式学习记录/4. timerwheel/2. 多级时间轮$ gcc mutiTimeWheel.c -lpthread
toney@ubantu:/mnt/hgfs/em嵌入式学习记录/4. timerwheel/2. 多级时间轮$ ./a.out 
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100

从结果可以看出:如果添加的定时任务是比较耗时的操作,那么后续的任务也会被阻塞,可能一直到超时,甚至一直阻塞下去,这个取决于当前任务是否耗时。这个理论上是绝不能接受的:一个任务不应该也不能去影响其他的任务吧。但是目前没有对此问题进行改进和完善,以后有机会再继续完善吧。

总结

到此这篇关于利用C语言实现经典多级时间轮定时器的文章就介绍到这了,更多相关C语言多级时间轮定时器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 利用C语言实现经典多级时间轮定时器

本文链接: https://www.lsjlt.com/news/130452.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 利用C语言实现经典多级时间轮定时器
    目录1. 序言 2. 多级时间轮实现框架2.1 多级时间轮对象2.2 时间轮对象2.3 定时任务对象2.4 双向链表 2.5 联结方式 3. 多级时间轮C语言实现 3.1 双向链表头...
    99+
    2024-04-02
  • 怎么用C语言实现经典多级时间轮定时器
    本篇内容介绍了“怎么用C语言实现经典多级时间轮定时器”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!多级时间...
    99+
    2024-04-02
  • C语言手写多级时间轮定时器
    目录为什么使用多级时间轮的方式单级时间轮多级时间轮头文件实现文件为什么使用多级时间轮的方式 有序队列实现定时器 添加/删除任务: 遍历每一个节点, 找到相应的位置插入, 因此时间复...
    99+
    2024-04-02
  • C++定时器实现和时间轮介绍
    目录定时器最小堆实现定时器时间轮单层级时间轮多层级时间轮定时器 有些时候我们需要延迟执行一些功能,比如每10s进行一次数据采集。或者告知用户技能冷却有多少时间,如果我们将执行这些功能...
    99+
    2024-04-02
  • 怎么使用Go语言实现时间轮
    本文小编为大家详细介绍“怎么使用Go语言实现时间轮”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么使用Go语言实现时间轮”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。时间轮概述时间轮是一种基于时间概念的循环缓...
    99+
    2023-07-05
  • 利用C语言实现经典游戏斗兽棋
    效果图 核心代码 #include<stdio.h> #include<easyx.h> #include<stdlib.h> #include...
    99+
    2024-04-02
  • c语言定时器功能怎么实现
    在C语言中,可以使用``头文件中的`sleep()`函数来实现简单的定时器功能。`sleep()`函数用于使程序暂停执行一段时间,参...
    99+
    2023-08-30
    c语言
  • 怎么用C语言实现扫雷经典游戏
    本篇内容介绍了“怎么用C语言实现扫雷经典游戏”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!C语言实现扫雷游戏,供大家参考,具体内容如下实现扫...
    99+
    2023-06-20
  • C语言如何实现古代时辰计时与现代时间换算
    这篇文章主要介绍“C语言如何实现古代时辰计时与现代时间换算”,在日常操作中,相信很多人在C语言如何实现古代时辰计时与现代时间换算问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C语言如何实现古代时辰计时与现代时...
    99+
    2023-07-05
  • C语言实现定时器控制LED灯闪烁
    本文实例为大家分享了C语言实现定时器控制LED灯闪烁的具体代码,供大家参考,具体内容如下 实现效果如图: 周期:2s; LED引脚为P2口。 #include<reg5...
    99+
    2024-04-02
  • 怎么使用C语言实现计时器
    本篇内容主要讲解“怎么使用C语言实现计时器”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用C语言实现计时器”吧!实现思路简单介绍一下我的实现思路:本文包括三个版本,分别是极简版、普通版、高...
    99+
    2023-06-25
  • 一盘王者的时间用C语言实现三子棋
    目录1.先进行环境的配置2.各种功能实现的逻辑关系2.1实现游戏的开始退出流程2.2 创建一个名为board的二维数组,并进行初始化2.3 棋盘的搭建2.4 玩家下棋,并打印新的棋盘...
    99+
    2024-04-02
  • C语言操作时间函数之怎么实现定时执行某个任务小程序
    本篇内容主要讲解“C语言操作时间函数之怎么实现定时执行某个任务小程序”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C语言操作时间函数之怎么实现定时执行某个任务小程序”吧!时间概述由上图可知:通过...
    99+
    2023-06-16
  • Go语言实现定时器的方法详解
    目录TimerTiker本文主要介绍了Go语言实现定时器的两个方法,包括一次性定时器(Timer)和周期性定时器(Ticker),一次性定时器:newTimer()创建一个能够往当前...
    99+
    2022-12-20
    Go语言实现定时器 Go语言定时器 Go 定时器
  • C语言实现时间处理工具的示例代码
    目录c语言-时间处理工具头文件功能实现c语言-时间处理工具 头文件 #ifndef STUDY_TIME_UTIL_H #define STUDY_TIME_UTIL_H lon...
    99+
    2024-04-02
  • 详解如何利用C#实现设置系统时间
    目录实践过程效果代码实践过程 效果 代码 public partial class Form1 : Form { public Form1() { ...
    99+
    2022-12-20
    C#设置系统时间 C#设置时间 C# 系统时间
  • 掌握Go语言文档中的time.Tick函数实现间隔定时器
    Go语言是一种功能强大、灵活性高的编程语言,其拥有丰富的标准库和文档,提供了许多实用的函数和工具。其中,time.Tick函数是Go语言中非常好用的一个函数,它可以帮助我们实现在一定时间间隔内执行某些代码的功能,即间隔定时器。本文将介绍如何...
    99+
    2023-11-03
    Go语言 timeTick 间隔定时器
  • 利用JS定时器实现元素移动
    利用JS定时器做一个元素做一个有移动效果的方法,实现思路:首先声明一个变量存放元素距离左侧的边距,然后我们在声明一个变量存放每次元素需要移动的距离,然后再给这个方法一个完成时间就可以...
    99+
    2024-04-02
  • 掌握Go语言文档中的time.NewTicker函数实现多次定时器
    掌握Go语言文档中的time.NewTicker函数实现多次定时器,需要具体代码示例Go语言是一门快速、简洁、高效的编程语言,它在并发编程上表现出色,具有强大的标准库支持。在Go语言的标准库中,提供了很多强大的时间处理函数,其中time包中...
    99+
    2023-11-04
    Go语言 timeNewTicker 多次定时器
  • C语言驱动开发内核枚举IoTimer定时器怎么实现
    本篇内容主要讲解“C语言驱动开发内核枚举IoTimer定时器怎么实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C语言驱动开发内核枚举IoTimer定时器怎么实现”吧!正文IoTimer内核定...
    99+
    2023-07-04
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作