Sleep实现

来源:互联网 发布:网络的利与弊作文800 编辑:程序博客网 时间:2024/05/16 07:03
gbool I_Sleep(gint32 un32Time)
{
    struct  timespec req, rem;
    //设置要suspend的时间长度
    req.tv_sec  = (long) un32Time / 1000;
    req.tv_nsec = (long) 1000000 * (un32Time % 1000);
    //注意,若线程suspend,则函数nanosleep不会返回,即线程进入suspend状态
   // 若返回0,则说明已经经过了req指定的时间了,否则返回-1
    while (nanosleep(&req, &rem) != 0)
    {
        //此时返回但不是0,说明是错误或者被信号中断
        //若被信号中断,则此时errno会被设置为EINTR,而输出参数rem会存储剩余的时间,这时,继续用剩余的时间设置为req 循环调用nanosleep
        if (errno == EINTR)
        {
            req = rem;
        }
       //其他错误,则返回-1,不继续进入循环了
       else
       {
           return gfalse;
       }
  }  //end of while


    return gtrue;


}
原创粉丝点击