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 8871533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:00:51+00:00 2026-06-14T18:00:51+00:00

I’m writing a small program that uses a certain percentage of CPU. The basic

  • 0

I’m writing a small program that uses a certain percentage of CPU. The basic strategy is that I will continuously check the CPU usage, and make the process sleep if the level of usage is higher than the given value.

Moreover, since I’m using MacOS(no proc/stat like Linux, no PerformanceCounter in C#), I have to execute top command in another thread and get the CPU usage from it.

The problem is I keep getting a very high usage of CPU even I give a small value as argument. And after several experiments, it seems caused by shared field by multithreads.

Here are my code(code 1) and experiments:

(code 2)Initially I thought it is the shell commands make the usage very high, so I commented the infinite loop in run(), leaving only the getCpuUsage() running. However, the CPU usage is nearly zero.

(code 3)Then, I wrote another run() function independent from the cpuUsage, which is intended to use 50% of CPU. It works well! I think the only difference between code 1 and code 3 is the usage of cpuUsage. So I’m wondering if sharing field between threads will use CPU a lot?

code 1

const char CPU_COMMAND[] = "top -stats cpu -l 1 -n 0| grep CPU\\ usage | cut -c 12-15";

int cpuUsage; // shared field that stores the cpu usage

// thread that continuously check CPU usage
// and store it in cpuUsage
void getCpuUsage() {
    char usage[3];
    FILE *out;
    while (1) {
        out = popen(CPU_COMMAND, "r");
        if (fgets(usage, 3, out) != NULL) {
            cpuUsage = atof(usage);
        } else {
            cpuUsage = 0;
        }
        pclose(out);
    }
}

// keep the CPU usage under ratio
void run(int ratio) {
    pthread_t id;
    int ret = pthread_create(&id, NULL, (void *)getCpuUsage, NULL);
    if (ret!=0) printf("thread error!");

    while (1) {
        // if current cpu usage is higher than ration, make it asleep
        if (cpuUsage > ratio) {
            usleep(10);
        }
    }

    pthread_join(id, NULL);
}

code 2

// keep the CPU usage under ratio
void run(int ratio) {
    pthread_t id;
    int ret = pthread_create(&id, NULL, (void *)getCpuUsage, NULL);
    if (ret!=0) printf("thread error!");

    /*while (1) {
        // if current cpu usage is higher than ration, make it asleep
        if (cpuUsage > ratio) {
            usleep(10);
        }
    }*/

    pthread_join(id, NULL);
}

code 3

void run() {
    const clock_t busyTime = 10;
    const clock_t idleTime = busyTime;

    while (1) {
        clock_t startTime = clock();
        while (clock() - startTime <= busyTime);
        usleep(idleTime);
    }
}
  • 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-06-14T18:00:52+00:00Added an answer on June 14, 2026 at 6:00 pm

    Does shared field in multithreading C program use CPU a lot?

    Yes, constant reads/writes to/from a shared memory locations, by multiple threads on multiple CPUs cause a cache line to constantly move between CPUs (cache bounce). IMO, it’s the single most important reason for poor scalability in naive “parallel” applications.

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

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.