Pthread sleep function, cpu consumption

On behalf of, sorry for my far from perfect English.

I recently wrote a daemon for myself for Linux (more precisely, an OpenWRT router) in C ++, and I came up with a problem.

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

Everything works fine, but my processor is always 100%. Now I have it 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 does things every 1 second. Time is not critical here. I know that the processor always checks if the time has passed. I tried to do something like this: while (clock () <endwait) USleep (200); But when the function compresses (and also decreases) the seam to freeze the clock increment (its always constant value after sleep mode).

Is there any solution, ready-made functions (e.g. phread_sleep (20ms)) or go around my problem? Maybe I should somehow get access to the main watch?

It’s not so important here, I can pretty much check how long the status check took (click clock () earlier, compare with after) and calculate the value that you want to put as an argument to the usleep function. But in another topic, I would like to use this form.

Do you smile to freeze the whole process?

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

Thanks for any answers and suggestions that really value him.

Jl

+1
c ++ pthreads sleep cpu-usage
source share
2 answers

I decided it differently.

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

Mention that this code is vulnerable to time changes at runtime. I have no idea how to omit this, but in my case it is not very important.

0
source share

If he does not need to be exactly 1 s, then just skip a second. usleep and sleep put the current thread in an effective wait state, which is at least the amount of time you requested (and then it can be scheduled again).

If you are not trying to get closer to the exact time, there is no need to check the clock ().

+2
source share

All Articles