<p>When should I <strong>not</strong> use the ThreadPool in .Net?</p>

<p>It looks like the best option is to use a ThreadPool, in which case, why is it not the only option?</p>

<p>What are your experiences around this?</p>

[{'answer_id': 10280, 'author': 'Quibblesome', 'author_id': 1143, 'author_profile': 'https://Stackoverflow.com/users/1143', 'pm_score': 1, 'selected': False, 'text': "<p>When you're going to perform an operation that is going to take a long time, or perhaps a continuous background thread.\nI guess you could always push the amount of threads available in the pool up but there would be little point in incurring the management costs of a thread that is never going to be given back to the pool.</p>\n"}, {'answer_id': 10286, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 3, 'selected': False, 'text': '<p>Thread pools make sense whenever you have the concept of worker threads.  Any time you can easily partition processing into smaller jobs, each of which can be processed independently, worker threads (and therefore a thread pool) make sense.</p>\n\n<p>Thread pools do not make sense when you need thread which perform entirely dissimilar and unrelated actions, which cannot be considered "jobs"; e.g., One thread for GUI event handling, another for backend processing.  Thread pools also don\'t make sense when processing forms a pipeline.</p>\n\n<p>Basically, if you have threads which start, process a job, and quit, a thread pool is probably the way to go.  Otherwise, the thread pool isn\'t really going to help.</p>\n'}, {'answer_id': 10287, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': True, 'text': '<p>The only reason why I wouldn\'t use the <code>ThreadPool</code> for cheap multithreading is if I need to&hellip;</p>\n\n<ol>\n<li>interract with the method running (e.g., to kill it)</li>\n<li>run code on a <a href="http://msdn.microsoft.com/en-us/library/system.stathreadattribute.aspx" rel="nofollow noreferrer" title="Single-Threaded Apartment thread; see e.g. this MSDN reference page for STAThreadAttribute">STA thread</a> (this happened to me)</li>\n<li>keep the thread alive after my application has died (<code>ThreadPool</code> threads are background threads)</li>\n<li>in case I need to change the priority of the Thread.  We can not change priority of threads in ThreadPool which is by default Normal.</li>\n</ol>\n\n<blockquote>\n  <p><strong>P.S.:</strong> The MSDN article <a href="http://msdn.microsoft.com/en-us/library/0ka9477y.aspx" rel="nofollow noreferrer" title="The Managed Thread Pool (page on MSDN)"><em>"The Managed Thread Pool"</em></a> contains a section titled, <em>"When Not to Use Thread Pool Threads"</em>, with a very similar but slightly more complete list of possible reasons for not using the thread pool.</p>\n</blockquote>\n\n<p>There are lots of reasons why you would need to skip the <code>ThreadPool</code>, but if you don\'t know them then the <code>ThreadPool</code> should be good enough for you.</p>\n\n<p>Alternatively, look at the new <a href="https://learn.microsoft.com/en-us/visualstudio/extensibility/debugger/parallel-extension-internals-for-the-dotnet-framework" rel="nofollow noreferrer">Parallel Extensions Framework</a>, which has some neat stuff in there that may suit your needs without having to use the <code>ThreadPool</code>.</p>\n'}, {'answer_id': 10292, 'author': 'Dogmang', 'author_id': 1135, 'author_profile': 'https://Stackoverflow.com/users/1135', 'pm_score': 3, 'selected': False, 'text': '<p>To quarrelsome\'s answer, I would add that it\'s best not to use a ThreadPool thread if you need to guarantee that your thread will begin work immediately. The maximum number of running thread-pooled threads is limited per appdomain, so your piece of work may have to wait if they\'re all busy. It\'s called "queue user work item", after all.</p>\n\n<p>Two caveats, of course:</p>\n\n<ol>\n<li>You can change the maximum number of thread-pooled threads in code, at runtime, so there\'s nothing to stop you checking the current vs maximum number and upping the maximum if required.</li>\n<li>Spinning up a new thread comes with its own time penalty - whether it\'s worthwhile for you to take the hit depends on your circumstances.</li>\n</ol>\n'}, {'answer_id': 10369, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 5, 'selected': False, 'text': "<p>@Eric, I'm going to have to agree with Dean.  Threads are expensive.  You can't assume that your program is the only one running.  When everyone is greedy with resources, the problem multiplies.</p>\n\n<blockquote>\n  <p>I prefer to create my threads manually and control them myself. It keeps the code very easy to understand.</p>\n</blockquote>\n\n<p>That's fine when it's appropriate.  If you need a bunch of worker threads, though, all you've done is make your code more complicated.  Now you have to write code to manage them.  If you just used a thread pool, you'd get all the thread management for free.  And the thread pool provided by the language is very likely to be more robust, more efficient, and less buggy than whatever you roll for yourself.</p>\n\n<blockquote>\n<pre><code>Thread t = new Thread(new ThreadStart(DoSomething));  \nt.Start();  \nt.Join();  \n</code></pre>\n</blockquote>\n\n<p>I hope that you would normally have some additional code in between <code>Start()</code> and <code>Join()</code>.  Otherwise, the extra thread is useless, and you're wasting resources for no reason.</p>\n\n<blockquote>\n  <p>People are way too afraid of the resources used by threads. I've never seen creating and starting a thread to take more than a millisecond. There is no hard limit on the number of threads you can create. RAM usage is minimal. Once you have a few hundred threads, CPU becomes an issue because of context switches, so at that point you might want to get fancy with your design.</p>\n</blockquote>\n\n<p>A millisecond is a <em>long</em> time on modern hardware.  That's 3 million cycles on a 3GHz machine.  And again, you aren't the only one creating threads.  Your threads compete for the CPU along with every other program's threads.  If you use not-quite-too-many threads, and so does another program, then together you've used too many threads.</p>\n\n<blockquote>\n  <p>Seriously, don't make life more complex than it needs to be. Don't use the thread pool unless you need something very specific that it offers.</p>\n</blockquote>\n\n<p>Indeed.  Don't make life more complex.  If your program needs multiple worker threads, don't reinvent the wheel.  Use the thread pool.  That's why it's there.  Would you roll your own string class?</p>\n"}, {'answer_id': 10410, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': -1, 'selected': False, 'text': "<p>@Eric</p>\n\n<blockquote>\n  <p>@Derek, I don't exactly agree with the scenario you use as an example. If you don't know exactly what's running on your machine and exactly how many total threads, handles, CPU time, RAM, etc, that your app will use under a certain amount of load, you are in trouble.</p>\n</blockquote>\n\n<p>Are you the only target customer for the programs you write?  If not, you can't be certain about most of that.  You generally have no idea when you write a program whether it will execute effectively solo, or if it will run on a webserver being hammered by a DDOS attack.  You can't know how much CPU time you are going to have.</p>\n\n<p>Assuming your program's behavior changes based on input, it's rare to even know exactly how much memory or CPU time your program will consume.  Sure, you should have a pretty good idea about how your program is going to behave, but most programs are never analyzed to determine exactly how much memory, how many handles, etc. will be used, because a full analysis is expensive.  If you aren't writing real-time software, the payoff isn't worth the effort.</p>\n\n<p>In general, claiming to know exactly how your program will behave is far-fetched, and claiming to know everything about the machine approaches ludicrous.</p>\n\n<blockquote>\n  <p>And to be honest, if you don't know exactly what method you should use: manual threads, thread pool, delegates, and how to implement it to do just what your application needs, you are in trouble.</p>\n</blockquote>\n\n<p>I don't fully disagree, but I don't really see how that's relevant.  This site is here specifically because programmers don't always have all the answers.</p>\n\n<blockquote>\n  <p>If your application is complex enough to require throttling the number of threads that you use, aren't you almost always going to want more control than what the framework gives you?</p>\n</blockquote>\n\n<p>No.  If I need a thread pool, I will use the one that's provided, unless and until I find that it is not sufficient.  I will not simply assume that the provided thread pool is insufficient for my needs without confirming that to be the case.</p>\n\n<blockquote>\n  <p>I'm not speaking as someone with only theoretical knowledge here. I write and maintain high volume applications that make heavy use of multithreading, and I generally don't find the thread pool to be the correct answer.</p>\n</blockquote>\n\n<p>Most of my professional experience has been with multithreading and multiprocessing programs.  I have often needed to roll my own solution as well.  That doesn't mean that the thread pool isn't useful, or appropriate in many cases.  The thread pool is built to handle worker threads.  In cases where multiple worker threads are appropriate, the provided thread pool should should generally be the first approach.</p>\n"}, {'answer_id': 10413, 'author': 'Will Dean', 'author_id': 987, 'author_profile': 'https://Stackoverflow.com/users/987', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <p>I'm not speaking as someone with only\n  theoretical knowledge here. I write\n  and maintain high volume applications\n  that make heavy use of multithreading,\n  and I generally don't find the thread\n  pool to be the correct answer.</p>\n</blockquote>\n\n<p>Ah, argument from authority - but always be on the look out for people who might be on the Windows kernel team.</p>\n\n<p>Neither of us were arguing with the fact that if you have some specific requirements then the .NET ThreadPool might not be the right thing.  What we're objecting to is the trivialisation of the costs to the machine of creating a thread.</p>\n\n<p>The significant expense of creating a thread at the raison d'etre for the ThreadPool in the first place.  I don't want my machines to be filled with code written by people who have been misinformed about the expense of creating a thread, and don't, for example, know that it causes a method to be called in every single DLL which is attached to the process (some of which will be created by 3rd parties), and which may well hot-up a load of code which need not be in RAM at all and almost certainly didn't need to be in L1.</p>\n\n<p>The shape of the memory hierarchy in a modern machine means that 'distracting' a CPU is about the worst thing you can possibly do, and everybody who cares about their craft should work hard to avoid it.</p>\n"}, {'answer_id': 11928310, 'author': 'hwiechers', 'author_id': 5883, 'author_profile': 'https://Stackoverflow.com/users/5883', 'pm_score': 0, 'selected': False, 'text': '<p>MSDN has a list some reasons here:</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/0ka9477y.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/0ka9477y.aspx</a></p>\n\n<blockquote>\n  <p>There are several scenarios in which it is appropriate to create and\n  manage your own threads instead of using thread pool threads:</p>\n  \n  <ul>\n  <li>You require a foreground thread.</li>\n  <li>You require a thread to have a particular priority.</li>\n  <li>You have tasks that cause the thread to block for long periods of time. The thread pool has a maximum number of threads, so a large\n  number of blocked thread pool threads might prevent tasks from\n  starting.</li>\n  <li>You need to place threads into a single-threaded apartment. All ThreadPool threads are in the multithreaded apartment.</li>\n  <li>You need to have a stable identity associated with the thread, or to dedicate a thread to a task.</li>\n  </ul>\n</blockquote>\n'}, {'answer_id': 16308855, 'author': 'supercat', 'author_id': 363751, 'author_profile': 'https://Stackoverflow.com/users/363751', 'pm_score': 1, 'selected': False, 'text': "<p>Threadpool threads are appropriate for tasks that meet both of the following criteria:</p>\n\n<ol>\n<li>The task will not have to spend any significant time waiting for something to happen\n<li>Anything that's waiting for the task to finish will likely be waiting for many tasks to finish, so its scheduling priority isn't apt to affect things much.\n</ol>\n\n<p>Using a threadpool thread instead of creating a new one will save a significant but bounded amount of time.  If that time is significant compared with the time it will take to perform a task, a threadpool task is likely appropriate.  The longer the time required to perform a task, however, the smaller the benefit of using the threadpool and the greater the likelihood of the task impeding threadpool efficiency.</p>\n"}]
['https://Stackoverflow.com/questions/10274', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/380/']