2

I have an application (forever running Daemon application) which has to manage communication (request and response) between physical devices (example RS485 devices) and applications inside a system - something like a "communication layer" for many applications to talk to hardware devices.

Applications_Send_Request -> Communication_Layer_Forwards_To_Devices -> Communication_Layer_Gets_Response -> Application_receives_response.

The part where I fwd the request to a device and get response can be an asynchronous job.

I'm planning to create a thread which will do this in the background which the application accepts other requests.

Now, this thread will be short lived - about few milliseconds to a few seconds only.

I might get a few hundreds of requests every second - so the application is creating lots of short lived threads and deleting them - doing it forever continuously.

Concerns: 1 - Creating thousands of threads ( though they are very short lived ). 2 - Run time of application while it does is very long - runs forever.

Is the above design OK and safe when threads are used very heavily like this.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
bkj
  • 109
  • 3
  • 13
  • 3
    You can use a thread pool – Low Flying Pelican Oct 28 '15 at 00:34
  • 1
    Your idea basically _is_ a thread pool. If you use `Task` or `Task` to schedule your work, that will use a ThreadPool as well. You can check the MSDN documentation here: https://msdn.microsoft.com/en-us/library/system.threading.threadpool(v=vs.110).aspx – vesan Oct 28 '15 at 00:42
  • Check out this article on creating a CustomThreadPool. It is very verbose, however, similar patterns are used for windows services that need to maintain a constant state of operation. http://www.codeproject.com/Articles/548865/Create-a-Custom-ThreadPool-in-Csharp – Ross Bush Oct 28 '15 at 00:42
  • 1
    Every thread you create consumes at least 1MB of RAM, so you will find creating lots of short-lived threads will be very time and memory consuming. The thread-pool is your friend. – Enigmativity Oct 28 '15 at 00:44
  • 1
    Thanks all for the suggestions. Appreciate it. – bkj Oct 28 '15 at 01:44

1 Answers1

2

Use the Windows thread pool for this, exposed via System.Threading.ThreadPool.

Threads on Windows are extremely expensive to spin up and down manually. They take a large amount of user and kernel memory even when idle, and are pretty heavyweight to maintain.

Windows provides the Thread Pool APIs specifically to solve the problem of running a large number of independent short-lived parallel tasks on different threads, so that programs don't need to overload the system by creating their own. A really good discussion on choosing the thread pool can be found here: https://stackoverflow.com/a/230023

Your programs will be smaller, faster, less complex, and easier to maintain if you simply let the OS manage your threads for you.

Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54