25

TPL uses Task Schedulers to coordinate tasks. According to official document, default task scheduler uses Thread Pool, but if TaskCreationOptions.LongRunning option is presented then it will create a dedicated thread for that task (A).

Question: As of now MSDN documents for Visual Studio 2010 are not ready and current online MSDN is not finalized; does anyone knows if (A) is true or false?

Kaveh Shahbazian
  • 13,088
  • 13
  • 80
  • 139

2 Answers2

38

Yes, LongRunning forces the creation of a new thread outside the pool. Here's some pseudo-disassembled code from the latest framework version:

...
if (task.Options HasFlag LongRunning) then
    create new Thread thread
    thread.Start(task)
...

Edit: converted from ugly C# to pseudocode.

Mau
  • 14,234
  • 2
  • 31
  • 52
  • 7
    Reflectoring is kind-of an accepted practice for the Fx itself here. But the danger is: this could change in a future version. – H H Jun 23 '10 at 23:06
  • 1
    @chiba: I don't see a problem with Mau did. I think it's helpful to see the disassembly. +1 for Henk as well. This is not guaranteed to be on its own thread as the scheduler logic may change. – Scott P Jun 23 '10 at 23:11
  • 1
    @Scott I think that you'll find that in theory the owners of the library from which this code was disassembled could issue a [take-down notice](http://en.wikipedia.org/wiki/Online_Copyright_Infringement_Liability_Limitation_Act#Takedown_example) to the website that hosted it - it's copyright Infringement. So it's generally not good practice. I'm thinking of good behaviour and SO here. – Tim Lloyd Jun 23 '10 at 23:16
  • 5
    @Chibacity Well the question was about how things are implemented. A final documentation page should state nothing less than what is said here. Plus, you will find that that is not the actual source code since noone would write the if condition like that. Anyhow, changing the answer to pseudocode. – Mau Jun 23 '10 at 23:16
  • @Mau Presumably you are aware that derivative work still infringes copyright? What we do in our own space is different from publishing on a public website. – Tim Lloyd Jun 23 '10 at 23:25
  • 8
    @chibacity: There also is such a concept as "fair use", even the original version might have been entirely legal. But I think none of us are experts on [US] copyright laws. – H H Jun 24 '10 at 09:04
  • Does this still apply if you try to do a very large number of Tasks using the LongRunning option (say 1,000)? Will it create 1,000 threads? – Zaid Masud May 21 '12 at 17:47
  • @zooone9243: It will. Your machine might not like it :-) – Mau Jul 10 '12 at 13:58
  • For those who are worried about decompiled code. It's clear from the docs that it avoids the thread pool: http://msdn.microsoft.com/en-us/library/dd997402.aspx to quote: "... By using this option you avoid the ThreadPool completely, including the global and local queues." – Tom Peplow Nov 06 '12 at 08:02
15

Presumably you can check this by using "Thread.IsThreadPoolThread":

http://msdn.microsoft.com/en-us/library/system.threading.thread.isthreadpoolthread.aspx

Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130