zoukankan      html  css  js  c++  java
  • usleep sleep函数会重置clock 的返回值

    今天在看hypertable的异步通讯库的过程中发现,usleep, sleep 函数会重置 clock的返回值,很是奇怪。

    这里记录一下,再给一个例子: 

    下面的代码是能正常运行的,但是当我调整了sleep的位置后,程序就永远不能动了。


     #include <stdio.h>

    #include <time.h>
    #include 
    <unistd.h>

    void wait ( int seconds )
    {
        clock_t endwait;
        endwait 
    = clock () + seconds * CLOCKS_PER_SEC ;
        
    while (clock() < endwait) {
        }   
    }

    int main ()
    {
        
    int n;
        printf (
    "Starting countdown...\n");
        
    for (n=10; n>0; n--)
        {   
            printf (
    "%d\n",n);
            wait (
    1);
            usleep(
    10);
        }   
        printf (
    "FIRE!!!\n");
        
    return 0;
    }

     #include <stdio.h>

    #include <time.h>
    #include 
    <unistd.h>

    void wait ( int seconds )
    {
        clock_t endwait;
        endwait 
    = clock () + seconds * CLOCKS_PER_SEC ;
        
    while (clock() < endwait) {
            usleep(
    10);
        }
    }

    int main ()
    {   
        
    int n; 
        printf (
    "Starting countdown...\n");
        
    for (n=10; n>0; n--)
        {   
            printf (
    "%d\n",n);
            wait (
    1);
        }
        printf (
    "FIRE!!!\n");
        
    return 0;
    }
  • 相关阅读:
    09 shell脚本程序练习
    springboot整合vue03-创建springboot工程
    springboot整合vue02-创建前端页面
    springboot整合vue01-创建vue工程
    pe工具04-获取数据目录
    pe工具03-获取节表信息
    pe工具02-解析pe头信息
    pe工具01-获取进程和模块
    以挂起方式创建进程
    进程
  • 原文地址:https://www.cnblogs.com/welkinwalker/p/2128712.html
Copyright © 2011-2022 走看看