Re: Threading in Order?
- From: Peter Duniho <no.peted.spam@xxxxxxxxxxxxxxxxxx>
- Date: Tue, 01 Dec 2009 09:32:53 -0800
Adam Benson wrote:
What makes you say that? The time stamps are out of order? Or something else?Unfortunately, the messages are not stored in order of occurrence.
messages are stored in the order
If you want to ensure that if thread A calls the logging method just before thread B then thread A's message goes in the log first do something like this (pseudo code) : [code snipped]
Nope. You simply cannot do that. Each thread can get interrupted by the OS at arbitrary points. Just because thread A calls the LogAMsg() method before thread B does, that doesn't mean that thread A will acquire the lock before thread B; it could happen to get pre-empted just before acquiring the lock, with thread B running immediately after and acquiring the lock before thread A.
The best one can do is not generate a timestamp until the lock has actually been acquired, but then the timestamp winds up being not the time of the event being logged, but the time that the event _is_ logged. A subtle, but VERY important difference.
If you want the timestamp to reflect the time that the event actually _happened_, then it is not possible to guarantee with 100% certainty that the event is added to the event queue in the same order in which the event happened. You can reorder the events in the log once logging is done and you know no more events will be added, but while the logging is happening, it's just not possible. The thread scheduler is controlling things at a lower level than the managed application can affect.
Pete
.
- Follow-Ups:
- Re: Threading in Order?
- From: Adam Benson
- Re: Threading in Order?
- References:
- Re: Threading in Order?
- From: Adam Benson
- Re: Threading in Order?
- Prev by Date: Re: base vs this
- Next by Date: Re: XML vs SQL Server
- Previous by thread: Re: Threading in Order?
- Next by thread: Re: Threading in Order?
- Index(es):
Relevant Pages
|