threaded vs asynchronous

Started by
5 comments, last by cerberus 24 years, 7 months ago
Hi,

Well, I came upon the same questions some time ago. I dont have an absolute anwser, but..
I used new threads. But I also wrote a function that timesout the recv(), and I used the nonblocking mode (winsock2, the ioctl()). Personaly, I prefer this solution, lets your programe do more things at the same time, and you can also use threads to do easy pipline treatments.

But I have looked in some game codes with socketSpy, and QuakeWorld uses nonblocking mode sockets. They may be used for timeouts, but somehow, I think not. I think QW dosent use many threads, so, is my solution really the best... but it still is the one I would chose in most cases

DlooB

Advertisement
Thats how we do it. A packet in queue feed by a blocking thread handling a single clients connection. Not sure how this scales up, as for each additonal client another thread has to be created. Thinking massive multiplayer, 1000s of threads wouldnt do at all, but our game isnt that type of game, so were not too concerned. You might want to consider it though. However i dont know how polling 1000s of sockets is any better.

-ddn

Is this for a client application or a server application?

If it's for a server, the "message queue" or "round robin" approach is good. It provides fast response and doesn't require any complex threading or resource handling.

------------------
DavidRM
Samu Games

The thread approach get my "thumbs up". Multiple sockets is a waste of resources + a security nightmare, and windows events are simply too slow.

For massive multiplayer you'd need a different approach altogether, similar to the way HTTP servers deal with massive amounts of clients - that is one thread per request, out of a given thread pool. When the pool size is exceeded, clients are put on hold...

/NJ

<b>/NJ</b>
Server side:

Asynchronous approach - I'm not a big fan of it. The code is pretty Windows-specific. Most socket samples you'll find on the net are blocking. Plus keeping track of all the async calls you've got going can be a headache. And its not that much of an improvement performance-wise- at some level, there is threading/async processing going on within the TCP/IP stack. In general, not worth it.

Message queue/round robin approach. This is good as long as each request can be served in a predictable manner. Ideally as fast as possible but you want predictablility over speed. Say you have two algorithms for handling requests - number one takes 200 ms on average but once in a while takes 2 seconds. Number two takes less than 500 ms every time. Even though on average number one is faster, number two is preferred because you have a gauranteed response time of 500 ms. If you get a request that takes a long time to serve it kills performance for everyone.

Thread-per-client approach. Best for when client requests are independent of each other, such as in web server. Can get complicated when requests require significant processing of shared state - the locking code can be complex,prone to deadlocks, and cause scalability chokepoints. The overhead of allocating threads can be dealt with by pre-allocating a pool of threads. Normally "unused" threads are kept suspended until they are grabbed from the pool and used as a client thread. When a client disconnects its servicing thread is returned to the pool.

Process-per-client - All of the above methods suffer from a flaw - if the request servicing code crashes, the whole server goes down and everyone has a bad day. Increased reliablility can be had at the cost of some overhead by having each request service routine in a separate process - if one crashes, it doesn't affect other clients. I know what you are thinking - I'm Mr. Bad-Ass Network Coder and my code never crashes. Well maybe so, but can you say that for the underlying OS? Or how about malicious attacks that exploit security holes in your protocol and bring down the server? Do you really want to have 1000 angry users when some script kiddie figures out an exploit or maybe just a few?

I'm sure the last two methods could be combined to give the efficiency of threading with the robustness of forking processes.

------------------
-vince

-vince


Before I launch into the network code of this game I'm working on, I need to decide a few things. One thing I'm having trouble with is winsock and blocking/non-blocking stuff. I've written network code before that used WSAAsyncSelect()...it worked well enough but I didn't like relying on windows messaging to inform me when a network event happened. Would it make more sense to use blocking sockets in seperate threads? That way a recv() wouldn't hold up the whole program while it waited for data. Any thoughts? Thanks in advance!
Glad to hear that I'm not the only one who thinks that mulitple processes can be a viable solution - In my last job I did a client/server solution that used forking processes, and had a terrible time convincing customers that it was NOT a problem - somebody must have convinced them that a server HAS to be threaded to scale ... Oh, the persistence of ignorance - SIGH!

/NJ

<b>/NJ</b>

This topic is closed to new replies.

Advertisement