Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6735659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:02:19+00:00 2026-05-26T11:02:19+00:00

On behalf, sorry for my far from perfect English. I’ve recently wrote my self

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:02:20+00:00Added an answer on May 26, 2026 at 11:02 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Preface: I'm posting this on behalf of a friend (who's apparently to shy to
I'm asking this on behalf of a certain corporation: http://www.microsoft.com/visualstudio/en-us/watch-it-live Things to bear in
I'm trying to submit an update of an existing application on behalf of one
Asking on behalf of my coworker: We're working on a Facebook app that uses
My sample webapp makes secure calls to webservices on behalf of its clients, this
A major component of our application sends email to members on behalf of other
Case 1(Trailing space) > on behalf of all of us .split(/\W+/) => [on, behalf,
I´m faced with what is probably just a small oversight on my behalf, but
In this video (approx. 31 minutes in), Crockford says they (speaking on behalf of
(Asking this on behalf of a member of our Bay Area R Group. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.