Is there an alternative for sleep() in C?

后端 未结 16 1874
无人及你
无人及你 2020-12-02 14:55

In traditional embedded programming, we will give a delay function like so:

for(i=0;i<255;i++)
   for(j=0;j<255;j++);

In the micropro

相关标签:
16条回答
  • 2020-12-02 15:40

    any decent C compiler would, without extra work, remove your code entirely and the delay would vanish

    0 讨论(0)
  • 2020-12-02 15:40

    available in linux usleep( int microseconds ) nanosleep( ... ) more precision see man pages for calling arguments

    0 讨论(0)
  • 2020-12-02 15:40

    an attempt... to really solve this issue, i.e. something that works (not like the above attempts of answer) lol

    I still have to improve this code to make it out. Few add-ons are welcome.

    // Sleep for both Windows and Linux: 
    // Too bad? No one proposed you a solution that works? 
    // Since Windows has no select.h nor poll.h, an implementation
    // is necessary.
    //  
    // Solutions on boards are often refered to use either select or poll, but ok, what about C (not c++)?
    //
    /// implementation of poll is destined in this attempt for windows
    /// Ideally, you add this part of code to the header of you *.c file, and it might work through...
    
    #ifdef WIN32
    #include <time.h>
    #include <sys/time.h>
    #include <ws2tcpip.h>
    #include <Winsock2.h>
    #include <windows.h>
    /* winsock doesn't feature poll(), so there is a version implemented
     * in terms of select() in mingw.c. The following definitions
     * are copied from linux man pages. A poll() macro is defined to
     * call the version in mingw.c.
     */
    #define POLLIN      0x0001    /* There is data to read */
    #define POLLPRI     0x0002    /* There is urgent data to read */
    #define POLLOUT     0x0004    /* Writing now will not block */
    #define POLLERR     0x0008    /* Error condition */
    #define POLLHUP     0x0010    /* Hung up */
    #define POLLNVAL    0x0020    /* Invalid request: fd not open */
    struct pollfd {
      SOCKET fd;        /* file descriptor */
      short events;     /* requested events */
      short revents;    /* returned events */
    };
    
    int mingw_poll (struct pollfd *, unsigned int, int);
    
    #define poll(x, y, z)        mingw_poll(x, y, z)
    #endif
    
    
    
    
    
    int mingw_poll(struct pollfd *fds, unsigned int nfds, int timo)
    {
        struct timeval timeout, *toptr;
        fd_set ifds, ofds, efds, *ip, *op;
        int i, rc;
    
        /* Set up the file-descriptor sets in ifds, ofds and efds. */
        FD_ZERO(&ifds);
        FD_ZERO(&ofds);
        FD_ZERO(&efds);
        for (i = 0, op = ip = 0; i < nfds; ++i) {
        fds[i].revents = 0;
        if(fds[i].events & (POLLIN|POLLPRI)) {
            ip = &ifds;
            FD_SET(fds[i].fd, ip);
        }
        if(fds[i].events & POLLOUT) {
            op = &ofds;
            FD_SET(fds[i].fd, op);
        }
        FD_SET(fds[i].fd, &efds);
        } 
    
        /* Set up the timeval structure for the timeout parameter */
        if(timo < 0) {
        toptr = 0;
        } else {
        toptr = &timeout;
        timeout.tv_sec = timo / 1000;
        timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000;
        }
    
    #ifdef DEBUG_POLL
        printf("Entering select() sec=%ld usec=%ld ip=%lx op=%lx\n",
               (long)timeout.tv_sec, (long)timeout.tv_usec, (long)ip, (long)op);
    #endif
        rc = select(0, ip, op, &efds, toptr);
    #ifdef DEBUG_POLL
        printf("Exiting select rc=%d\n", rc);
    #endif
    
        if(rc <= 0)
        return rc;
    
        if(rc > 0) {
            for (i = 0; i < nfds; ++i) {
                int fd = fds[i].fd;
            if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds))
                fds[i].revents |= POLLIN;
            if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds))
                fds[i].revents |= POLLOUT;
            if(FD_ISSET(fd, &efds))
                /* Some error was detected ... should be some way to know. */
                fds[i].revents |= POLLHUP;
    #ifdef DEBUG_POLL
            printf("%d %d %d revent = %x\n", 
                    FD_ISSET(fd, &ifds), FD_ISSET(fd, &ofds), FD_ISSET(fd, &efds), 
                    fds[i].revents
            );
    #endif
            }
        }
        return rc;
    }
    
    0 讨论(0)
  • 2020-12-02 15:42

    One common mechanism is to use a select() that is guaranteed to time out, and specify the sleep time as the timeout:

    // Sleep for 1.5 sec
    struct timeval tv;
    tv.tv_sec = 1;
    tv.tv_usec = 500000;
    select(0, NULL, NULL, NULL, &tv);
    

    The select() is typically used to check a set of file descriptors and wait until at least one is ready to perform I/O. If none is ready (or, in this case, if no fds are specified), it will time out.

    The advantage of select() over a busy loop is that it consumes very little resources while sleeping, while a busy loop monopolizes the processor as much as permitted by its priority level.

    0 讨论(0)
提交回复
热议问题