This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

C6678 sleep function

Hi all,

in my c6678 NDK program, I have 2 tasks. At some point, one of them is in a loop like this:

while (continue())
    ;

while the second task is receiving data from a TCP socket:

while(1)
{
    i = (int)recvnc(...);
    ...
    // Depending on the received data, my continue() function may return 0.
}

The problem is that I never receive data on the second task. In fact, wireshark shows me that the packets sent to the
board are being resent because no ACK is given from NDK. It seems to me that, since a thread is saturated with the "while(continue()) ;"
NDK cannot work properly.

So, I would like to change it to something like:

while (continue())
    Sleep_microseconds(50);

However, I cannot find any sleep function in the documentation. All I have found in the examples is TaskSleep, but it seems to work
only with times above the millisecond. Is there a sleep function which I can use to wait for a specified number of microseconds, allowing NDK to do its part properly?

Thanks


  • HIMO, You should change approach. Usually RTOS schedule timeouts with the resolution of the Reat-time clock, that can be set by the user but normally is some milliseconds.

    You could change the priority of the receiver, so to be higher then the worker thread. This should work, but a busy-wait worker thread it is not a good practice in multi-thread application. A think the better solution is to use a SYS/BIOS event to synch the threads (Event_pend()/Event_Post())

  • Alberto is right, your approach is not the best idea.

    BTW, if you want to add some delay, use this function:

    void addDelay (Int32 count)
    {
        UInt32      value;

        if (count <= 0)
            return;

        /* Get the current TSC  */
        value = TSCL;

        while ((TSCL  - value) < (UInt32)count);
    }

    Calling addDelay(1000) in your program you will have put your task to "sleep" for 1000 CPU cycles.

    Bye

  • Thanks Alberto and Samuele.

    I think the Samuele solution would not work in my case. Although I could wait for a specified time, the thread would be working for that time (not sleeping).

    I will check the Event_pend and Event_post functions that Alberto pointed out.

    On the other hand, I think I have found a way to wait, sleeping, for some time with microseconds precission. Basically, I can open a socket and make a call to select() to check for data, specifying a timeout in microseconds. On the other side of the socket, the client never sends data. So, the select() should be like a "sleep" call. It can be a waste of resources, but I think it could work until I adapt the code to the Event functions.