Multi-threading & Listening Sockets
-
I have written a Windows Service which creates multiple threads. Each thread listens on a seperate port, writes received messages to disk and acknowledges the receipt of each message. It works perfectly with a single thread so I started load testing multiple threads with disapointing results. I've noticed erratic behavior and am pretty sure its due to the multi-threading. This is my first true multithreaded app and wanted to get some pointers. My Windows Service basically just creates (with ThreadPool.QueueUserWorkItem) and stops the threads as necessary. The rest of the code (Listening and responding, storing port numbers and directory paths, writing messages to disk) is contained in a DLL shared by each thread. So first question is: 1. Is what I'm attempting to accomplish possible, or is it going to be a locking nightmare ? The reason I ask is that it appears I need to lock the listening loop since it is contained in a DLL used by each thread. But won't this effectively stop the rest of the the threads from listening to their ports since it is blocking? Do I need to ditch the idea of listening on multiple ports within threads and just have multiple instances of the same app on a server ? Any help would be appreciated.
-
I have written a Windows Service which creates multiple threads. Each thread listens on a seperate port, writes received messages to disk and acknowledges the receipt of each message. It works perfectly with a single thread so I started load testing multiple threads with disapointing results. I've noticed erratic behavior and am pretty sure its due to the multi-threading. This is my first true multithreaded app and wanted to get some pointers. My Windows Service basically just creates (with ThreadPool.QueueUserWorkItem) and stops the threads as necessary. The rest of the code (Listening and responding, storing port numbers and directory paths, writing messages to disk) is contained in a DLL shared by each thread. So first question is: 1. Is what I'm attempting to accomplish possible, or is it going to be a locking nightmare ? The reason I ask is that it appears I need to lock the listening loop since it is contained in a DLL used by each thread. But won't this effectively stop the rest of the the threads from listening to their ports since it is blocking? Do I need to ditch the idea of listening on multiple ports within threads and just have multiple instances of the same app on a server ? Any help would be appreciated.
You know, it might help to look at this article[^]. :) It looks kinda like what you're trying to do...but it seems like he's done all the hard work for you.
Once you wanted revolution Now you're the institution How's it feel to be the man?