Thread hardware_concurrency() function in C++

Thread::hardware_concurrency is an in-built function in C++ std::thread. It is an observer function which means it observes a state and then returns the corresponding output. This function returns the number of concurrent threads supported by the available hardware implementation. This value might not always be accurate.

Syntax:

thread::hardware_concurrency()

Parameters: This function does not accept any parameters.

Return Value: It returns a non-negative integer denoting the number of concurrent threads supported by the system. If the value is either not computable or not well defined it returns 0.

Below program demonstrate the use of std::thread::joinable()



Note: On the online IDE this program will show error. To compile this, use the flag “-pthread” on g++ compilers compilation with the help of command “g++ –std=c++14 -pthread file.cpp”.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate the use of
// std::thread::hardware_concurrency()
  
#include <chrono>
#include <iostream>
#include <thread>
using namespace std;
  
int main()
{
    unsigned int con_threads;
  
    // calculating number of concurrent threads
    // supported in the hardware implementation
    con_threads = thread::hardware_concurrency();
  
    cout << "Number of concurrent threads supported are: "
         << con_threads << endl;
  
    return 0;
}

chevron_right


Possible Output

Number of concurrent threads supported are: 4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.