使用GetTickCount()和Sleep():
Code#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