Hi,

I'm trying to create a multiplayer game and I'm wondering if it will be useful to use threads and also how to use them.

There would be the following actions in the game (on linux server):
1) server checking if clients are still connected
2) collision detection
3) processing players' input
4) sending the result data to clients
5) ... and probably many more

My questions are:
1) How many threads can be created? (I know that if there is too many, they have to wait for their turn, but is there a limit?)
2) Lets say I would create a new thread for each player (let's say 1000 or more). I suppose it makes no sense, but would than the switching between threads be very time consuming?

Bye

Recommended Answers

All 4 Replies

I don't think that creating a new process for every player is the right approach in your case. In order to manage your threads correctly you have to do a bunch of shared data checking (google locks, semaphores, atomic operations). This will take some of the processors time every time a context switch has been made, so having thousands of threads is not optimal.

I'm pretty sure that you should be able to do just fine with just 3-4 threads. One or two threads for sending/receiving data, one timed thread for calculating physics, and one thread running at processor's speed for drawing and playing music.

>>How many threads can be created? (I know that if there is too many, they have to wait for their turn, but is there a limit?)

Windows 2000: maximum is 65,535 handles. That will include threads, files handles, GDI object handles, etc. I have not found if XP, Vista or Win7 are any different.

Thanks.

And is switching between threads time consuming?

Yes it is time consuming. Microsoft does not recomment more than a few threads per process.


How many threads are too many? Maybe you should read this thread. And this one too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.