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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:49:46+00:00 2026-06-06T17:49:46+00:00

When a simple Thread is run with just an infinite loop in it causing

  • 0

When a simple Thread is run with just an infinite loop in it causing 100% CPU, how come is this possible?

My thread call is as follows on a QEvent in Qt dialog class, Say on a button click.

  pthread_t thread_id;
  pthread_create( &thread_id, NULL, DataCollectionThread, (void*) this );

And my thread procedure is,

void* DataCollectionThread( void* pParam )
{
   ((m_DataCollection*)pParam)->m_ReadDatafromport();
   return NULL;
}

And this ReadData() contains…

while(1)
{
}

My requirement is collecting data from serial port and plot a graph continuously. But as the CPU usage is 100%, any hardware interrupt in between plotting cause the plotting to stall, as the CPU switches the task to handle interrupt.

I am calling this thread in a Qt::Dialog based class. I am pretty sure that nothing other than this is triggered. What’s wrong with this? Does a simple infinite loop causes 100% CPU consumption eh? Or Is there any problem with using pthread_create in Qt?

EDIT:For Jonathon Reinhart

This is the actual while loop

while( 1 )
    {

            while(( Dataisavailable))
            {
                 //push the read data to stack
            }



        if(!m_DataReadable)
            break;
      }
  • 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-06T17:49:47+00:00Added an answer on June 6, 2026 at 5:49 pm

    One simple hack to get around this: Go to sleep for (short) periods to let the CPU do other stuff. #include <ctime> and add somewhere in your loop:

    struct timespec ts;
    ts.tv_sec=0;
    ts.tv_nsec=10000000; // 10 milliseconds
    nanosleep(&ts, NULL);
    

    Of course, it would be better if you could explicitly sleep until you have actual work to do (more input to read, a full queue to trim). But adding in short sleeps will probably be sufficient.

    It might make sense to look into your m_pDataProvider object’s implementation. Check for or add a method allowing you to sleep until there’s more data. If you’re just reading from a character device (e.g. ttyS0), poll or select might be useful here.

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

Sidebar

Related Questions

I have a simple exercise where I call a Thread and subsequently the run()
The following code is a simple thread game, that switches between threads causing the
I am just trying to compile and run a very simple test program, but
Ok this may be really simple but everything I try just seems to hit
This is just a general question on actual thread design. I'm using Java on
I am implementing simple thread that passes messages to main UI thread to make
I declared a field: WriteableBitmap colorBitmap; Then I created a simple thread which does
I have a simple program that creates a thread, loops twenty times and then
I have a simple message schema where a thread ties 2 or more users
I have a simple message schema where a thread ties 2 or more users

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.