Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

.net - Is it true that for long running processes it is better to do thread manually instead of threadpool?

I read on the other day that for long-running tasks my best bet is to manually create threads instead of using .NET’s thread pool or Task Parallel. I'd really like someone to enlighten me as I am learning about c# threading, specially for long running IO tasks. Thank you in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That is true. The thread pool is optimised for small units of work and you can interfere with other work by holding onto a thread pool thread.

My rule of thumb is if an operation can take more than a second, it should not be on a thread pool thread. That is probably quite long.

Although this is undocumented, if you start a Task with TaskCreationOptions.LongRunning then a new Thread will be started to run the Task.

For most IO tasks, there are asynchronous versions of the framework methods that you should really use. These make use of kernel functions and mean that you won't be blocking any thread.

As always, I recommend reading Joe Albahari's free ebook, followed by Joe Duffy's Concurrent Programming on Windows. The later is 1000 pages long, but is full of useful details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...