views:

90

answers:

2

Background: "The Task Parallel Library (TPL) is a set of public types and APIs in the System.Threading and System.Threading.Tasks namespaces in the .NET Framework version 4. The purpose of the TPL is to make developers more productive by simplifying the process of adding parallelism and concurrency to applications."

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  A: 

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

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

chibacity
Just tried that, with the LongRunning option the property is false.
Henk Holterman
@Henk Excellent - so when using "LongRunning", "Thread.IsThreadPoolThread=false". Cheers.
chibacity
+1  A: 

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
-1 Why do people insist on disassembling other people's code and publishing it without permission.
chibacity
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](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.
chibacity
@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
@Mau Please see my comment.
chibacity
@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.
chibacity
@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
Follow-up question here: http://stackoverflow.com/questions/3109357
Henk Holterman