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
c ++ pthreads sleep cpu-usage
user1004212
source share