使用GetTickCount()和Sleep():

 

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

unsigned long GetTickCount()
{
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC,&ts); //LOCK_REALTIME, CLOCK_MONOTONIC
    return (ts.tv_sec*1000 + ts.tv_nsec/(1000*1000));
}

int main( int argc,char* argv[])
{
    //50%  
    int busyTime=10;
    int idleTime=10;
    unsigned long startTime = 0;

    while(true)
    {  
        startTime=GetTickCount();
        while((GetTickCount()-startTime)<=busyTime)
        {  
            ;
        }
        usleep(idleTime * 1000);
    }
    return 0;
}

 

参考链接:http://blog.csdn.net/wesweeky/article/details/6402564

相关文章:

  • 2021-11-24
  • 2021-12-14
  • 2021-06-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-15
  • 2021-11-23
猜你喜欢
  • 2021-05-20
  • 2021-12-04
  • 2022-12-23
  • 2022-03-04
  • 2022-12-23
  • 2022-01-22
  • 2021-06-19
相关资源
相似解决方案