c++ - A faster thread blocks slower thread -
i'm working on point cloud viewer, , design based on 2 thread
- first thread updates point cloud data ( 10 fps)
- second thread d3d renderer render point set screen (about 90 fps)
so code looks this:
std::shared_ptr<pointcloud> pointcloud; critical_section updatelock; void firstthreadproc() { while(true) { /* algorithm processes point cloud, takes time */ entercriticalsection(&updatelock); pointcloud->update(data,length,...); //also takes time copy , process leavecriticalsection(&updatelock); } } /*...*/ std::shared_ptr<d3drenderer> renderer; void secondthreadproc() { msg msg = { 0 }; while (wm_quit != msg.message) { if (peekmessage(&msg, null, 0, 0, pm_remove)) { translatemessage(&msg); dispatchmessage(&msg); } else { entercriticalsection(&updatelock); renderer->render(pointcloud); leavecriticalsection(&updatelock); } } }
i thought second thread way more fast first one, when first 1 entered critical section, second 1 blocked, renderer window should freeze or then. i'm observed right renderer window runs smooth, camera rotate or zoom in/out, good, first thread unstable, fps ranging 10 fps 1 fps.
i'm thinking 2 point cloud buffers, first thread updates second buffer when outsides critical section, swap 2 buffers within critical section. work?
as mentioned in this, critical_section not provide first-in, first-out(fifo) ordering. since second thread way more fast first thread, , whole loop critical section, enter critical section right after leave it. may in critical section , keep first 1 out of it.
my solution put more job of second thread outside critical section, works fine.
Comments
Post a Comment