0

Can i open some threads from inside a thread?

I have a Windows service whit 2 different roles. When the service starts, I open 2 threads: one for each role. In each thread I need to open sub threads.

How many threads can I open for each core?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • If i open Windows process monitor i see that the process "Sistem" has 175 thread. – user2119955 Mar 01 '13 at 09:46
  • It is generally the case that as a developer you needn't worry about the hardware architecture. All encapsulation paradigms point in that direction. He or she should worry about scalability and being a good developer. Each core can run a lot of threads. I suggest you experiment a bit with it yourself and see how it goes (it's not going to make your computer explode)... (Don't start by creating 10000 threads, start with 50). Is the total number of threads known at design time ? As long as the number is bounded you'll have no problem. Experiment with it, see how it goes... – Eduard Dumitru Mar 01 '13 at 09:52

2 Answers2

1

Sure you can instantiate other threads from a thread - as for how many this may prove useful : Optimal number of threads per core

It's worth keeping in mind that threads come with a cost of their own, and adding threads to increase performance only works to a point.

Community
  • 1
  • 1
NDJ
  • 5,189
  • 1
  • 18
  • 27
1

There is no hard limit... is as long as framework has sufficient resources and free handles etc. In past one of my app ran about 150 threads at peak.

Within spawn threads, we used to spawn more threads as needed which use to execute tasks we needed processing.

After a while we moved to using ThreadPool as its less resource intensive and there is a 1024 thread allocation on app start

Remember each thread when active gets scheduled by OS scheduler and do you want many hot threads. so when not doing anything, either put those threads to sleep or if using ThreadPool just exit current iteration to release the thread back to the pool

For .NET Framework its worth reading these

Threads and Threading

http://msdn.microsoft.com/en-us/library/6kac2kdh.aspx

Using Threads and Threading

http://msdn.microsoft.com/en-us/library/e1dx6b2h.aspx

Having said that be careful of what you create. You might need to worry about thread safety, locking, synchronization however you want to put it. Debugging the app also becomes a bit more difficult

Hermit Dave
  • 3,036
  • 1
  • 13
  • 13
  • Good that you explicitly stated to put threads in a sleep state when not needed, so you the OS can take them off the ready queue. That said, 1024 threads from the ThreadPool is a lot... And there is a hard limit to the thread numeber, at least on 32 bit systems, which is around 2k threads http://blogs.msdn.com/b/oldnewthing/archive/2005/07/29/444912.aspx – Lorenzo Dematté Mar 01 '13 at 10:14
  • 2k threads limit is a lot of threads.. I am sure before you hit 2k, the system will crash due to some other reason :) – Hermit Dave Mar 01 '13 at 10:33
  • no, it does not crash. Read the link I provided. You can even reach an higher thread count, if you decrease stack space. It is useseless, I know, but why should the system crash? – Lorenzo Dematté Mar 01 '13 at 10:35
  • I didn't mean that.. real coding and use of threads is more than PoC.. and remember .NET threads are different.. they run within CLR... – Hermit Dave Mar 01 '13 at 10:37
  • I agree that 2k (or 1k, or 200 hundred..) of threads is nonsense. Still, I do not see why the system would crash. Maybe your app, but the OS? No way... – Lorenzo Dematté Mar 01 '13 at 10:58
  • on my bad.. meant to say that app :) not the system.. let me correct that – Hermit Dave Mar 01 '13 at 10:59
  • And .NET threads need one OS thread to run; I know that the host is free to change OS thread affinity, but I suspect this is not done by the current "standard" CLR. Anyway, this should increase an hypothetical max thread count, not lower it – Lorenzo Dematté Mar 01 '13 at 11:00
  • 'App' makes much more sense! With such an high thread count, I bet it will grind to an halt sooner or later – Lorenzo Dematté Mar 01 '13 at 11:01
  • i've solved the problem making number of threads configurable – user2119955 Mar 01 '13 at 12:45