Linux threads occupy high CPU location analysis

Source: Internet
Author: User
Tags high cpu usage

Today, my friend asked me a Linux program CPU consumption limit, how to analyze,

High CPU consumption, simulating high CPU usage

The first piece of code:

1#include <iostream>2#include <thread>3#include <vector>4 5 6 intMainintargcChar**argv) {7   8Std::vector<std::thread>test_threads;9      for(inti =0; I <9; i++){Ten Test_threads.push_back (Std::thread ([]{ One      while(1){ AStd::this_thread::sleep_for (Std::chrono::milliseconds ( -)); -     } -       })); the     } - Test_threads.push_back (Std::thread ([]{ -        while(1){ -std::cout<<"CPU"<<Std::endl; +       } -     })); +      A      for(Auto &x:test_threads) { at X.join (); -     } -      -     return 0; -}

The 10th thread does not sleep, it will monopolize the process of the time slice, resulting in high CPU utilization,

Now we need to locate 10th.

First step: Top View program Process ID

Step two: Top-h-P 96263 locates CPU-intensive thread IDs

Step three: Use Pstack 96263 or strace-f-P 96263 to locate the thread stack

Strace-f-P 96263

Linux threads occupy high CPU location analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.