Conceptual question about Lidgren lib and Threads

206 views
Skip to first unread message

Llorenç Marti

unread,
Oct 25, 2010, 7:31:23 AM10/25/10
to lidgren-network
Hi all

I have a conceptual question about using threads and Lidgren lib:
(imagine we use gen 3)

Is it correct to have 2 or mores threads processing in parallel the
incoming messages that lidgren give to us with function
'ReadMessage(...)' ??

I am confused because i dont know if 2 or mores threads can cause a
conceptual error processing these messages. Imagine that second
message needs to be processed in order after first one, but for other
reasons, one free thread process it before the other busy thread.
I think this error can araise very often, because if 2 or more threads
are fighting to process this messages, nobody can say wich one will be
processed first.

So, i think that another aproach is to have only one (1) thread that
process all messages from 'ReadMessage(...)', but if one of this
messages needs more computational time like database access or other
thigs, then we can use other threads to achive it in a parallel way.

I am correct or i miss something ?

Thanks for your help :)

LLORENS

lidgren

unread,
Oct 25, 2010, 7:37:22 AM10/25/10
to lidgren-network
Sounds like you are correct.

ReadMessage is thread safe; it will return messages in the correct
order without problems; however, if you have multiple thread consuming
messages they can make no assumptions on the order the messages are
processed in (of course - since they're processed in parallell)

I think your second approach is better. Process messages on just one
thread (preferably the main thread unless it's very heavy) - and if
the processing of a certain message can be assumed to take a long
time; use the thread pool to spawn a thread to process it separately.

Lidgren already have an internal thread for networking so unless you
have to; I'd try to avoid threading unless it's really necessary.

--michael

Enriko Riba

unread,
Oct 25, 2010, 9:06:45 AM10/25/10
to lidgren...@googlegroups.com

Hi Lorens,

This is not a very good idea. It is much more performant to have a dedicated
reader which dispatches the messages into queues (one per worker thread).
Furthermore the receiving lidgren thread raise san event once there are new
messages available so your dedicated reader has virtually no impact on
CPU/thread starvation.

You can do this with something lite this:

AutoResetEvent waitDataSignal = netSvr.MessageReceivedEvent;
do{
if ((receivedMsg = netSvr.ReadMessage()) != null)
{
switch (receivedMsg.MessageType)
{
// ... DO WHATEVER YOU DO
}
}
else
waitDataSignal.WaitOne(150); // some timeout so that after 150 ms
we can check the end condition and terminate
} while (isMyServerRunning);

HTH
Riki

Hi all

LLORENS

--
You received this message because you are subscribed to the Google Groups
"lidgren-network" group.
To post to this group, send email to lidgren...@googlegroups.com.
To unsubscribe from this group, send email to
lidgren-netwo...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/lidgren-network?hl=en.

_____


<< ella for Spam Control >> has removed 335 Spam messages and set aside 122
Newsletters for me
You can use it too - and it's FREE! www.ellaforspam.com


ArKangeL ArK

unread,
Oct 25, 2010, 4:39:45 PM10/25/10
to lidgren...@googlegroups.com

Hi and thanks for your info about that :)

LLORENS
Reply all
Reply to author
Forward
0 new messages