Question

I have code that logs execution times of routines by accessing QueryPerformanceCounter. Roughly:

var
    FStart, FStop : Int64 ;
...
QueryPerformanceCounter (FStart) ; 
... <code to be measured>
QueryPerformanceCounter (FStop) ; 

<calculate FStop - FStart, update minimum and maximum execution times, etc>

Some of this logging code is inside threads, but on the other hand, there is a display UI that accesses the derived results. I figure the possibility exists of the VCL thread accessing the same variables that the logging code is also accessing. The VCL will only ever read the data (and a mangled read would not be too serious) but the logging code will read and write the data, sometimes from another thread.

I assume QueryPerformanceCounter itself is thread-safe.

The code has run happily without any sign of a problem, but I'm wondering if I need to wrap my accesses to the Int64 counters in a critical section?

I'm also wondering what the speed penalty of the critical section access is?

Était-ce utile?

La solution

Any time you access multi-byte non-atomic data across thread when both reads and writes are involved, you need to serialize the access. Whether you use a critical section, mutex, semaphore, SRW lock, etc is up to you.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top