omer schleifer
omer schleifer

Reputation: 3935

Controlling actual concurrency with tasks

There are some scenarios where I would like to be able to control the actual concurrency when using tasks.

A good example for this is when writing a small client for load testing the server side API. In that case I would like to have X concurrent requests at any time.

Now , if I use TPL , I would only be able to set max degree of parallelism , which is not the same. I thought about using long running tasks, but from what I read that's not recommended:

https://social.msdn.microsoft.com/Forums/en-US/8304b44f-0480-488c-93a4-ec419327183b/when-should-a-taks-be-considered-longrunning?forum=parallelextensions

Of course I can use threads instead of tasks , but if there is an option to achieve actual concurrency using TPL , I would prefer it.

Upvotes: 3

Views: 169

Answers (1)

Jason Hernandez
Jason Hernandez

Reputation: 2969

If you want direct control over the number threads running a specific piece of code you should not use TPL. TPL has optimizations going on under the hood like Task Inlining and Work Stealing. Even if you managed to get the correct number of threads going at the same time, you might not generate the expected amount of load. Especially if the tasks spend most of their time waiting, such as in an http or wcf request.

A semaphore would probably suffice.

Upvotes: 1

Related Questions