为什么不像Linux上的mutithread中的usleep那样工作呢?

问题描述 投票:0回答:1

在下面的程序中,如果我将usleep替换为sleep(0.5),那么它就不会像usleep一样进入睡眠状态,这是什么原因?我尝试搜索但没有找到答案,这在我的Ubuntu 16.04和CentOS 8中都会发生。预先感谢。

#include<unistd.h>
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
void*  mmmm(void* args){
        int i=0;
        while(i<1000){
                printf("A:%d\n",i);
                i++;
                usleep(500000);
        //      sleep(0.5);
        }
        return NULL;
}
void* nnnn(void* args){
        int j=1000;
        while(j>0){
                printf("B:%d\n",j);
                j--;
                usleep(500000);
//              sleep(0.5);
        }
        return NULL;
}
int main(){
        pthread_t a,b;
        int errora = pthread_create(&a,NULL,mmmm,NULL);
        int errorb = pthread_create(&b,NULL,nnnn,NULL);
        printf("error A:%d,error B:%d\n",errora,errorb);

        int status_a,status_b;
        pthread_join(b,(void*)&status_b);
        pthread_join(a,(void*)&status_a);
        return 0;
}

c linux pthreads
1个回答
1
投票

要休眠的参数是无符号的int秒。 0.5生成警告并假定0

““男人3睡”:

unsigned int sleep(unsigned int seconds);

© www.soinside.com 2019 - 2024. All rights reserved.