-
Threads question
Hello all,
I am writing a program that will be multi-user and have implimented sockets, one of the things I will have the program do is scan the Arraylist of sockets and check to make sure they are still connected, and if not then remove them from the program.
My question is, should I do this in a seperate thread or put it loop into the main game while true loop?
I ask this because I already use 2 seperate threads (the main flow loop and a socket connection loop) and I don't yet know if if there is such a thing as too many seperate threads running at the same time?
My assumption is that the threads are less strain on the program than a linear writing style but I was hoping for some clarification..
Thank you for any advise
Randor
-
Re: Threads question
There can be such as thing as too many threads, but this is typically if one spawns dozens or hundreds for tasks that should only need a few. If you feel (or have evidence) that the work will bog down any other important process in the same thread, then create a new thread to do the work in parallel. In other words, yes create a new thread.
I think to best address your concern is to do some trial and error in a controlled environment. Create a small program which performs some intense operations and play with the number of threads it uses - watch the processors through your OS and see how the number of threads affects the strain on your OS.
-
Re: Threads question
To add to copeg's excellent advice, one way to control the number of threads is to use one of the various shades of thread pool, and you might consider using this for the threads that the server uses to communicate with its many sockets.
-
Re: Threads question
-
Re: Threads question
If you are memory constrained watch out on creating too many threads. As suggested use a thread pool if needed - also check the thread stack size.