Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many threads can I run concurrently?

A comment to another of my questions says that I can only run "so many" threads concurrently, a notion which I have seen elsewhere.

As a threading novice, how can I determine the maximum number of threads to use? Or is this a "how long is a piece of string" question? What does it depends on? Hardware config or what?

(VB in MS Visual Studio with .Net 3.5, if that matters)


Update: is anyone aware of any s/w tool which could suggest a number of threads (or tasks), or should I just code my own which keeps trying different numbers until throughput drops?


[Upperdate] Almost seven years later & we now have a software recommendations site, so I asked if there is a tool to help with this.

like image 885
Mawg says reinstate Monica Avatar asked Jan 28 '11 12:01

Mawg says reinstate Monica


People also ask

What are the maximum number of threads that can be run concurrently?

What I know is that the maximum number of threads that can run concurrently on a normal CPU of a modern computer ranges from 8 to 16 threads. On the other hand, using GPUs thousands of threads can run concurrently without the scheduler interrupting any thread to schedule another one.

How many threads can be run at once?

Each CPU core can have two threads. So a processor with two cores will have four threads. A processor with eight cores will have 16 threads. A processor with 24 cores (yes, those exist), will have 48 threads.

Can multiple threads run concurrently?

Concurrency and Parallelism In the same multithreaded process in a shared-memory multiprocessor environment, each thread in the process can run concurrently on a separate processor, resulting in parallel execution, which is true simultaneous execution.

How many processes can run concurrently?

Yes multiple processes can run simultaneously (without context-switching) in multi-core processors. If all processes are single threaded as you ask then 2 processes can run simultaneously in a dual core processor.


2 Answers

It depends on hardware as you're (probably) not using a theoretical computer but a physical hardware one, so you have limited resources.

Read: Does Windows have a limit of 2000 threads per process?

Furthermore, even if you could run 5000+ threads, depending on your hardware, that could run much slower than a 10 thread equivalent program. I think you should take a look at thread pooling.

like image 59
Trinidad Avatar answered Sep 28 '22 19:09

Trinidad


Typically, the number of threads the run truly concurrently is determined by the number of CPUs and CPU cores (including hyper threading) you have. That is to say that at any given time the number of threads running (in the operating system) is equal to the number of "cores".

How many threads you can run concurrently in your app depends on a large number of factors. The best (lay man's) number would be the number of cores on the machine but of course that's like pretending no one (no other application) else exists :).

Frankly, I'd say do a lot more study on multi-threading in .NET/Windows because one tends to do more "damage" than good when one doesn't have a really solid understanding. .NET has the concept of a thread pool and you need to know how that works in addition to Windows.

In .NET 3.5/4.0 you should be looking at Tasks (Task Parallel Library) as the library does a much better job of determining how many threads (if at all) to spawn. With the TPL the threadpool gets a major overhaul and it is a lot smarter about spawning threads and task stealing etc. But you typically work with Tasks and not threads.

This is a complex area and as a result, the .NET framework introduced Tasks so as to abstract programmers from threads and therefore allowing the runtime to be smart about this while the programmer just say what she wants and not so much about how to do it.

like image 33
Shiv Kumar Avatar answered Sep 28 '22 17:09

Shiv Kumar