pthread sleep function, cpu consumption

人盡茶涼 提交于 2019-12-13 18:58:31

问题


On behalf, sorry for my far from perfect English.

I've recently wrote my self a demon for Linux (to be exact OpenWRT router) in C++ and i came to problem.

Well there are few threads there, one for each opened TCP connection, main thread waiting for new TCP connections and, as I call it, commander thread to check for status.

Every thing works fine, but my CPU is always at 100%. I now that its because of the commander code:

void *CommanderThread(void* arg)
{
    Commander* commander = (Commander*)arg;
    pthread_detach(pthread_self());
    clock_t endwait;

    while(true)
    {
        uint8_t temp;
        endwait = clock () + (int)(1 * CLOCKS_PER_SEC);
        for(int i=0;i<commander->GetCount();i++)
        {
            ptrRelayBoard rb = commander->GetBoard(i);
            if (rb!= NULL)
                rb->Get(0x01,&temp);
        }

        while (clock() < endwait);
    }
    return NULL;
}

As you can see the program do stuff every 1s. Time is not critical here. I know that CPU is always checking did the time passed. I've tried do do something like this: while (clock() < endwait) usleep(200); But when the function usleep (and sleep also) seam to freeze the clock increment (its always a constant value after the usleep).

Is there any solution, ready functions (like phread_sleep(20ms)), or walk around for my problem? Maybe i should access the main clock somehow?

Here its not so critical i can pretty much check how long did the execution of status checking took (latch the clock() before, compare with after), and count the value to put as an argument to the usleep function. But in other thread, I would like to use this form.

Do usleep is putting whole process to freeze?

I'm currently debugging it on Cygwin, but don't think the problem lies here.

Thanks for any answers and suggestions its much appreciated.

J.L.


回答1:


If it doesn't need to be exactly 1s, then just usleep a second. usleep and sleep put the current thread into an efficient wait state that is at least the amount of time you requested (and then it becomes eligible for being scheduled again).

If you aren't trying to get near exact time there's no need to check clock().




回答2:


I've I have resolved it other way.

#include <sys/time.h>
#define CLOCK_US_IN_SECOND 1000000
static long myclock()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return (tv.tv_sec * CLOCK_US_IN_SECOND) + tv.tv_usec;
}
void *MainThread(void* arg)
{
    Commander* commander = (Commander*)arg;
    pthread_detach(pthread_self());
    long endwait;

    while(true)
    {
        uint8_t temp;
        endwait = myclock() + (int)(1 * CLOCK_US_IN_SECOND);
        for(int i=0;i<commander->GetCount();i++)
        {
            ptrRelayBoard rb = commander->GetBoard(i);
            if (rb!= NULL)
                rb->Get(0x01,&temp);
        }
        while (myclock() < endwait)
            usleep((int)0.05*CLOCK_US_IN_SECOND);
    }
    return NULL;
}

Bare in mind, that this code is vulnerable for time change during execution. Don't have idea how to omit that, but in my case its not really important.



来源:https://stackoverflow.com/questions/7830243/pthread-sleep-function-cpu-consumption

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!