Brian Gideon said:
I did some more thinking on this. I misspoke. My solution already
already guarentees relative ordering for every thread. That's because
it's impossible for a thread to enqueue messages out of order relative
to itself.
No message logging implementation should ever have a problem with message
*from a single thread* being out of order. Someone would have to go to
*extra* work to make that a possibility.
So I don't really see what you're trying to say here. You might as well say
that a thread never has to worry about the program statements executed by
that thread ever executing out of order (ignoring for a moment CPU
implementations that do just that). It's trivially true, but not all that
interesting or useful to know.
In any case, whatever guarantees you can make using a "queue message, write
to log file later" implementation, you can just as easily make using a
"write to log file immediately" implementation. The underlying
implementation doesn't affect the question of what order log entries occur
in.
[...]
Hmm...I sort of agree. I do disagree on one important point. No one
cares that thread A races with thread B when logging. It doesn't
matter that if A does something first and then B does its thing next
that B's log message appears first in the log.
That's exactly what I said. How are you disagreeing with me?
What people do care
about is that A's log messages are written to the log in the order that
they occurred.
Yes, they do care about that. However, that happens naturally in any
typical message logging implementation. Since the statements within a given
thread always execute in order, it is trivial to ensure that logged messages
from a given thread are always in order.
For example, if A performs tasks 1 and 2 then the log
message for 1 should be written before the log message for 2. A
solution using the ThreadPool won't guarentee that because the
persisting of log messages can be dispatched to different threads.
I have no idea why you think that the "persisting of log messages" would be
"dispatched to different threads". Certainly no one here has suggested
anything like that. You'd have to go to extra work to do that. The threads
aren't present for the purpose of logging messages...they are present for
the purpose of doing work. Any messages they log will necessarily occur in
the correct order, relative to each thread's own work.
Contrast that with my solution where log messages are dispatched to a
single thread.
Why dispatch a log message to a thread at all? Messages should be logged to
a data structure, if not written directly to a file, shared (and
synchronized, of course) by all threads using it.
A little code might help.
[...]
Notice that since there is only one thread removing from the queue the
order that messages are received by the Logger is the order they are
persisted. And it's impossible for any specific thread to queue its
messages out of order.
You could just as easily replace your queue and thread with a single "Log"
method that does the "Persist the message to a file, database, etc" work you
have delegated to a whole new thread. It would work just as well, from a
message ordering standpoint.
Pete