Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sleeping in a pooled C# thread

In this web tutorial on threading in C#, Joseph Albahari writes: "Don't go sleeping in pooled threads!" Why should you not do this? How badly can it affect performance? (It's not that I want to do it; I'm just curious.)

like image 296
user181813 Avatar asked Jun 10 '11 11:06

user181813


1 Answers

There are only a limited number of threads in the thread pool; thread pools are designed to efficiently execute a large number of short tasks. They rely on each task finishing quickly, so that the thread can return to the pool and be used for the next task.

So sleeping in a thread pool thread starves out the pool, which may eventually run out of available threads, and be unable to process the tasks you assign to it.

like image 162
jalf Avatar answered Oct 20 '22 13:10

jalf