Multi-Threaded Client???

Started by
14 comments, last by CtrlAltDel 20 years, 11 months ago
quote:Original post by Anonymous Poster
If you''re on the client, make your sockets non-blocking, and read them all each frame. There''s very little overhead in reading a nonblocking socket that has no data.

For servers, write a single thread, and have it select() over all sockets, reading from all the sockets that have data (they should still be non-blocking).

This is not only very efficient (both for client and server); it also ports very easily to UNIX.

Multi-threading is almost never worth it for something with as much inter-dependence as a game; the locking overhead will kill you.


i havent prged a mplyer game yet so i my not know what i''m talking about. but it would prb be better to have a seperate thread using blocking functions, so they wait until they get something and pipe it into whatever needs it. what locking overhead is there? if the critical section is tiny it should be better or the same as polling it.

www.autodefrost.com/~travisSignature Virus cleaned by McAfee
Advertisement
quack, that''s nonsense.

What the AP suggested is: One thread that polls the socket directly.

What you suggested is: One thread that reads from the socket and posts into a queue. One thread that polls the queue directly.

You don''t gain _anything_ by doing what you suggested. In fact, your suggestion is worse in every way. It requires more resources, makes the code bigger introducing potential bugs and increases latency problems (because of scheduling between the two threads).

cu,
Prefect
Widelands - laid back, free software strategy
Someone told me that select() was a bad API ever award. So which is the most common model of using asynchronous sockets for clients, WSAAsyncSelect()?
And if I create a thread to handle my own network messages, will it cost more overhead than windows messages? How about the approach? ( I used select() before ).
Never play with fire.
quote:Original post by Prefect
quack, that's nonsense.

What the AP suggested is: One thread that polls the socket directly.

What you suggested is: One thread that reads from the socket and posts into a queue. One thread that polls the queue directly.

You don't gain _anything_ by doing what you suggested. In fact, your suggestion is worse in every way. It requires more resources, makes the code bigger introducing potential bugs and increases latency problems (because of scheduling between the two threads).

cu,
Prefect


i haven't programmed a mplayer game yet, just socket programing. I meant one thread reads the data and the main game loop checks a q. Not two threads. my idea is the same as AP but instead of an asynch read it does a blocking read, the thread doesn't really have anything else to do.
Well i guess my game when i make in mplayer is gona be tougher then i thought










[edited by - quack on April 22, 2003 11:40:38 AM]

[edited by - quack on April 22, 2003 11:46:18 AM]
www.autodefrost.com/~travisSignature Virus cleaned by McAfee
What you suggest is still a program with _two_ threads, not one. The main loop runs in one of the threads (probably the original thread), while the blocking reads run in the second thread.

cu,
Prefect
Widelands - laid back, free software strategy
Quack, you are adding another (redundant) layer of abstraction when its already in place. If you set your sockets to non-blocking, then the OS kernel has taken the place of your blocking thread for reading the socket and filling the "queue" (in this case the queue is actually your socket''s recv buffer). It doesn''t make much sense to have a thread dedicated to do that again. However, if the receiving thread actually takes some action, like parsing the incoming data and building a web page, then it makes sense.

This topic is closed to new replies.

Advertisement