Jump to content
Posted 04 December 2012 - 12:35 PM
Posted 04 December 2012 - 01:53 PM
Edited by Zaoshi Kaba, 04 December 2012 - 01:55 PM.
Posted 04 December 2012 - 02:06 PM
I gets all your texture budgets!
Posted 05 December 2012 - 11:02 AM
Edited by aclysma, 05 December 2012 - 11:04 AM.
Posted 05 December 2012 - 03:26 PM
Posted 05 December 2012 - 03:47 PM
The easy solution is to use a lock or someone else's thread safe queue as has already been suggested. If you're just trying to solve a problem, this is definitely the way to go.
If you're interested in really digging into concurrent programming for the purpose of learning, you may want to investigate writing your own lockless queue where exactly one thread can write and exactly one thread can read. It doesn't require a ton of code to do something simple and it's well worth the learning experience. The need to pass data continuously from thread A to thread B in this way is extremely common. An easy implementation is to have a "read pointer" and a "write pointer" that wraps in a circular buffer - just be careful of order of operations and make the read pointer and write pointer volatile. The "pointer" can be an index into the static-sized circular buffer.
Using a queue like this is great for passing data to be processed. It can also proxy a function call to another thread by using the "command" pattern. You can even pass exceptions this way.
Posted 05 December 2012 - 04:49 PM
Posted 06 December 2012 - 04:28 PM
Posted 06 December 2012 - 05:37 PM
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.
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.
Posted 06 December 2012 - 08:58 PM
Quoted for emphasis.
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.