linuxÔõôʵÏÖ׼ʱÆ÷
linuxÖеÄ׼ʱÆ÷ʵÏÖ£º×¼Ê±Æ÷¹¤¾ß£º½¨É蹤¾ß£¬ÉèÖó¬Ê±Ê±¼äºÍ´¦Öóͷ£³ÌÐò£¬Ìí¼ÓÖÁÄںˡ£Äں˺¯Êý£ºÊ¹ÓÃalarm()½¨Éè·Ç׼ȷ׼ʱÆ÷¡¢sleep()»òusleep()ʹÀú³ÌÐÝÃß¡£Óû§¿Õ¼ä¿â£ºÊ¹ÓÃlibevent»òboost.asioµÈ¿â½¨ÉèºÍÖÎÀí׼ʱÆ÷¡£
ÔõÑùÔÚ Linux ÖÐʵÏÖ׼ʱÆ÷
׼ʱÆ÷¸ÅÊö
׼ʱÆ÷ÊÇÒ»ÖÖÄں˻úÖÆ£¬ËüÔÊÐíÔÚÌض¨µÄʱ¼ä¾àÀë»òÑÓ³ÙºóÖ´ÐÐʹÃü¡£ÔÚ Linux ÖУ¬¿ÉÒÔʹÓöàÖÖÒªÁìÀ´ÊµÏÖ׼ʱÆ÷¡£
ÒªÁì 1£º×¼Ê±Æ÷¹¤¾ß
׼ʱÆ÷¹¤¾ßÊÇ Linux ÖÐ×î³£ÓõÄ׼ʱÆ÷ʵÏÖ·½·¨¡£ËüÔÊÐíÄú½¨Éè׼ʱÆ÷¹¤¾ß£¬²¢Ö¸¶¨³¬Ê±Ê±¼äºÍ´¦Öóͷ£³ÌÐò¡£µ±×¼Ê±Æ÷ÓâÆÚʱ£¬´¦Öóͷ£³ÌÐò½«±»Å²Óá£
ʵÏÖ°ì·¨£º
½¨ÉèÒ»¸ö׼ʱÆ÷¹¤¾ß£º
struct timer_list my_timer;
µÇ¼ºó¸´ÖÆ
³õʼ»¯×¼Ê±Æ÷¹¤¾ß£º
init_timer(&my_timer);
µÇ¼ºó¸´ÖÆ
ÉèÖó¬Ê±Ê±¼ä£¨ÒÔºÁÃëΪµ¥Î»£©£º
my_timer.expires = jiffies + msecs_to_jiffies(TIMEOUT_MS);
µÇ¼ºó¸´ÖÆ
ÉèÖô¦Öóͷ£³ÌÐò£º
my_timer.function = my_timer_handler;
µÇ¼ºó¸´ÖÆ
Ìí¼Ó׼ʱÆ÷µ½Äںˣº
add_timer(&my_timer);
µÇ¼ºó¸´ÖÆ
ÒªÁì 2£ºÄں˺¯Êý
³ýÁË׼ʱÆ÷¹¤¾ß£¬Linux »¹ÌṩÁ˼¸¸öÄں˺¯ÊýÀ´½¨Éè׼ʱÆ÷¡£
alarm()£º½¨ÉèÒ»¸ö·Ç׼ȷµÄ׼ʱÆ÷£¬Ëü½«ÔÚÖ¸¶¨µÄʱ¼äºóÏòÀú³Ì·¢ËÍ SIGALRM Ðźš£
sleep()£ºÊ¹Ä¿½ñÀú³ÌÐÝÃßÖ¸¶¨µÄʱ¼ä¡£
usleep()£ºÊ¹Ä¿½ñÀú³ÌÐÝÃßÖ¸¶¨µÄ΢ÃëÊý¡£
ÒªÁì 3£ºÓû§¿Õ¼ä¿â
һЩÓû§¿Õ¼ä¿âÒ²ÌṩÁ˽¨ÉèºÍÖÎÀí׼ʱÆ÷µÄ¹¦Ð§¡£
libevent£ºÒ»¸ö¹¦Ð§¸»ºñµÄÊÂÎñ´¦Öóͷ£¿â£¬Ëü°üÀ¨Ò»¸ö¿çƽ̨µÄ׼ʱÆ÷ʵÏÖ¡£
Boost.Asio£ºÁíÒ»¸ö¿çƽ̨µÄÊÂÎñ´¦Öóͷ£¿â£¬Ëü¾ßÓÐ׼ʱÆ÷¹¦Ð§¡£
ÒÔÉϾÍÊÇlinuxÔõôʵÏÖ׼ʱÆ÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡