std::Queue and multithreading

Started by
11 comments, last by Hodgman 11 years, 5 months ago

I am not sure why you recommend a lock free ringbuffer, it will use a busy wait, won't it? A busy wait is seldom a good idea.

A busy wait is actually the best solution for situations with high contention where wait times tend to be quite low, because it ends up being less expensive then the context switch to a sleep state. However there is a break-even point where spinning is less efficient than sleeping. Really good implementations of concurrent data structures typically start with a busy wait, and eventually go to sleep if they wait too long.
Advertisement
Most good OSes will briefly spin-wait on mutexes/critical sections and then context switch as necessary. So reinventing that wheel is kind of a waste.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I strongly suggest not messing with it until you are highly proficient with concurrent systems and understand them deeply. Moreover, it requires a very rich comprehension of how the OS and even hardware work to do lockless correctly. IMHO, suggesting lockless programming to someone who is not already very experienced with concurrency is utterly bad advice.
Quoted for emphasis.
I thought I had a very professional level of understanding regarding multi-threaded software, so I went off and wrote my own lock-free queue. Took me a month of weekends of debugging until I thought I had something solid...
After using my queue with no problems for 6 months (and having the unit tests run and succeed every day), it's unit test finally failed one day, due to a small bug!
Now, I've had years of practice doing it, and I'm still quote anxious whenever I'm doing any lock-free stuff. Definitely get some practice and do your homework before stepping into that arena! Also, I've seen a lot of implementations that make use of dynamic memory ([font=courier new,courier,monospace]new[/font]/[font=courier new,courier,monospace]malloc[/font]/etc), which locks anyway, defeating the point.

This topic is closed to new replies.

Advertisement