3

What does this have to do with the threads of the processor? for example, an Intel i5 has four cores and four threads.

How many threads can we use in our program? for example using std :: thread (STL) in C ++?

is 8 threads a big or low number of threads for a program?

Impurity
  • 1,037
  • 2
  • 16
  • 31
  • 2
    It is the job of the operating system to make it *look* like the processor can execute an infinite number of threads. Just like it pretends that your program does not have to share the processor with other programs running on the machine. If you want to make the code efficient then you would consider to limit the number of threads you create to the number of cores so the OS does not have to do the extra work to context-switch between them. – Hans Passant Aug 18 '18 at 13:47

1 Answers1

3

It really depends. As a rule of thumb, limit the number of threads to something close to the number of cores (otherwise you might have too much context switchs). You might use std::thread::hardware_concurrency() as a hint. Often, you organize your program with a thread pool.

However, what really matters is the number of active threads. Some programs have hundreds of threads but are organized so that only a few of them are active (i.e. runnable) at any given instant, and most of them being idle (waiting for IO, or for some mutex or condition variable).

Be aware that a thread is a quite heavy resource (in particular because it has its own call stack, usually at least a megabyte). So don't have too many of them (hence, having thousands of threads is generally unreasonable, unless you have a very powerful and expansive computer).

The Intel hyper-threading technology is often disappointing. You probably don't want to have 8 active threads on an Intel processor with 4 cores and 8 hyperthreads. You need to benchmark, but you should expect some bad performance in such case (perhaps having 4 or 6 active threads would make your overall performance better).

Threads are an abstraction provided and managed by the operating system. So read Operating Systems: Three Easy Pieces to understand more about OSes.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    Fantastic response. Thank you! – Impurity Aug 18 '18 at 15:45
  • 1
    Re, "what really matters is the number of active threads..." Instead of trying to define "active" and "inactive," you could talk instead, about threads that exist to make parallel use of multiple CPUs (a.k.a., "worker threads", a.k.a., "compute-bound threads") vs. threads that exist mostly to wait for and handle events from different, unsynchronized sources. – Solomon Slow Aug 19 '18 at 00:45