Jump to content
static const int ThreadCount = std::thread::hardware_concurrency();

int main()
{
	std::thread CPUThread[ThreadCount];
}

I'm unable to use ThreadCount in the statement in main(), as it produces an error - only constants allowed. help me!!!

Nothing to see here ;)

Link to comment
https://linustechtips.com/topic/611861-clarification-needed-in-using-stdthread/
Share on other sites

Link to post
Share on other sites

std::thread::hardware_concurrency() is a function that gets run at runtime, to make a statically sized array like that you need a constant expression that can be evaluated at compile time not just a const value. This can be something like a literal number or a function declared as constexpr.

 

Instead of an array you can use a vector.

std::vector<std::thread> CPUThread(ThreadCount);

1474412270.2748842

Link to post
Share on other sites

2 hours ago, fizzlesticks said:

std::thread::hardware_concurrency() is a function that gets run at runtime, to make a statically sized array like that you need a constant expression that can be evaluated at compile time not just a const value. This can be something like a literal number or a function declared as constexpr.

 

Instead of an array you can use a vector.

std::vector<std::thread> CPUThread(ThreadCount);

Can you tell me how to implement constexpr, or std::vector? I'm new to all this!

 

Thanks!

Nothing to see here ;)

Link to post
Share on other sites

12 minutes ago, Anand_Geforce said:

Can you tell me how to implement constexpr, or std::vector? I'm new to all this!

 

Thanks!

To use a vector you replace your array line with the line I wrote and #include <vector> then you can use it just like an array.  You can learn more about vectors here.

constexpr won't work in this case because you have no control over the function you're trying to call (and the function has to be called at run time to make sense.)

 

1474412270.2748842

Link to post
Share on other sites

1 hour ago, fizzlesticks said:

To use a vector you replace your array line with the line I wrote and #include <vector> then you can use it just like an array.  You can learn more about vectors here.

constexpr won't work in this case because you have no control over the function you're trying to call (and the function has to be called at run time to make sense.)

 

But is it possible to create child threads using std::vector? I'm trying to create threads, where n = No. of Logical Processing Cores.

Nothing to see here ;)

Link to post
Share on other sites

1 hour ago, Anand_Geforce said:

But is it possible to create child threads using std::vector? I'm trying to create threads, where n = No. of Logical Processing Cores.

std::vector is essentially a dynamic array that has the memory managed for you. Simply push back the required number of thread objects. But judging by the fact that you don't know the syntax for arrays you need to learn more about the language before you jump into threading as its far from a beginner topic. It is probably one of the most complex topics available to programmers. The wrong design decision can have you reading a variable while it is still being written to!

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to post
Share on other sites

3 minutes ago, trag1c said:

std::vector is essentially a dynamic array that has the memory managed for you. Simply push back the required number of thread objects. But judging by the fact that you don't know the syntax for arrays you need to learn more about the language before you jump into threading as its far from a beginner topic. It is probably one of the most complex topics available to programmers. The wrong design decision can have you reading a variable while it is still being written to!

I just want to try - can you help me to do whatever you said (Sorry - I wasn't able to understand)?

Nothing to see here ;)

Link to post
Share on other sites

9 minutes ago, Anand_Geforce said:

I just want to try - can you help me to do whatever you said (Sorry - I wasn't able to understand)?

Exactly my point....In the future I would be willing to help but currently you're simply no where near ready. You need to progress further with the language its self and worry less about advanced topics. From your recent topics you need much simpler projects for your skill level as you seem to be jumping between projects that have probably been abandoned due to complexity and lack of know how.

 

Also you need to learn to use a search engine to find documentation. Simply google std::vector and you would find your answer.

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to post
Share on other sites

1 hour ago, trag1c said:

Exactly my point....In the future I would be willing to help but currently you're simply no where near ready. You need to progress further with the language its self and worry less about advanced topics. From your recent topics you need much simpler projects for your skill level as you seem to be jumping between projects that have probably been abandoned due to complexity and lack of know how.

 

Also you need to learn to use a search engine to find documentation. Simply google std::vector and you would find your answer.

You're right! I'm a total noon when it comes to Windows programming, or STL. I've tried to implement many ideas that come to my mind, but all of them have complicated code. I don't copy-paste code that I don't understand to a certain extent, and so many random projects get stalled. But I'd be grateful, if you could just help me with this project - How can I detect the number of logical cores on a system and create that many threads? I'm sorta creating a CMD-based CPU benchmarking utility. The single-threaded one was a success, but multi-threading is on... and it's a little difficult, but I'm not gonna give up so soon. So, if you can, please - help me with this... :)

 

Thanks!

Nothing to see here ;)

Link to post
Share on other sites

5 hours ago, trag1c said:

Exactly my point....In the future I would be willing to help but currently you're simply no where near ready. You need to progress further with the language its self and worry less about advanced topics. From your recent topics you need much simpler projects for your skill level as you seem to be jumping between projects that have probably been abandoned due to complexity and lack of know how.

 

Also you need to learn to use a search engine to find documentation. Simply google std::vector and you would find your answer.

BRAIN-WAVE! (At least Intellisense hasn't detected any errors yet)

 

I use getenv() to process the environment variable %NUMBER_OF_CORES%, assign it to an int, and in a switch statement, I implement the following...

 

switch(CoreCount)
{
  case 1:
  {
    std::thread t;
  }
  case 2:
  {
    std::thread t[2];
  }
  case 3:
  {
    std::thread t[3];
  }
  .
  .
  .
}

Let's hope this works!

Nothing to see here ;)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×