24

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?


2 답변


36

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.


  • Reflectoring is kind-of an accepted practice for the Fx itself here. But the danger is: this could change in a future version. - Henk Holterman
  • @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
  • @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 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
  • @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
  • @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. - Henk Holterman

14

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

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


  • Just tried that, with the LongRunning option the property is false. - Henk Holterman
  • @Henk Excellent - so when using "LongRunning", "Thread.IsThreadPoolThread=false". Cheers. - Tim Lloyd

Linked


Latest