c++ - Idendify the reason for a 200 ms freezing in a time critical loop -


new description of problem:

i run our new data acquisition software in test environment. software has 2 main threads. 1 contains fast loop communicates hardware , pushes data dual buffer. every few seconds, loop freezes 200 ms. did several tests none of them let me figure out software waiting for. since software rather complex , test environment interfere software, need tool/technique test recorder thread waiting while blocked 200 ms. tool useful achieve this?

original question:

in our data acquisition software, have 2 threads provide main functionality. 1 thread responsible collecting data different sensors , second thread saves data disc in big blocks. data collected in double buffer. typically contains 100000 bytes per item , collects 300 items per second. 1 buffer used write in data collection thread , 1 buffer used read data , save disc in second thread. if data has been read, buffers switched. switch of buffers seems major performance problem. each time buffer switches, data collection thread blocks 200 ms, far long. however, happens once in while, switching faster, taking no time @ all. (test pc: windows 7 64 bit, i5-4570 cpu @3.2 ghz (4 cores), 16 gb ddr3 (800 mhz)).

my guess is, performance problem linked data being exchanged between cores. if threads run on same core chance, exchange faster. thought setting thread affinity mask in way force both threads run on same core, means, lose real parallelism. idea let buffers collect more data before switching, dramatically reduces update frequency of data display, since has wait buffer switch before can access new data.

my question is: there technique move data 1 thread not disturb collection thread?

edit: double buffer implemented 2 std::vectors used ring buffers. bool (int) variable used tell buffer active write buffer. each time double buffer accessed, bool value checked know vector should used. switching buffers in double buffer means toggling bool value. of course during toggling reading , writing blocked mutex. don't think mutex possibly blocking 200 ms. way, 200 ms reproducible each switch event.

locking , releasing mutex switch 1 bool variable not take 200ms.

main problem 2 threads blocking each other in way. kind of blocking called lock contention. occurs whenever 1 process or thread attempts acquire lock held process or thread. instead parallelism have 2 thread waiting each other finish part of work, having similar effect in single threaded approach.

for further reading recommend this article read, describes lock contention more detailed level.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -