且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

pthread睡眠函数,cpu消费

更新时间:2022-10-15 15:23:08

我已经解决了其他方式。

  #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;
}

请记住,此代码在执行期间容易发生时间更改。没有想法如何省略,但在我的情况下它不是真的很重要。


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.

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.