openframeworks/openFrameworks — pull request #3295 by arturoc (2014-10-09 09:43:12+00:00)
Title: ofThreadChannel: easier multithreading

this implements a way of synchronizing threads that allows to avoid
locks in most cases making it way easier to work with multithreaded
applications.

instead of having some shared memory area + mutexes to lock, 2 threads
send messages from one to another through a channel. the channel
automatically does the locking internally. which makes it virtually impossible to
have crashes from wrong accesses since there's no accesses to shared
memory.

the receiving end of the channel can wait for a message to arrive
which effectively blocks the thread until there's new data to
process, avoiding consuming any CPU cycles at all if there's nothing
to do (ie: no need to call to ofSleep to throtle the thread CPU usage)

both ofURLFileLoader and ofxThreadedImageLoader are now implemented
using channels which simplifies their logic a lot. for example ofxThreadedImageLoader
threadedFunction goes from 30 lines of code to 10. there's also an
example in utils/threadedChannelExample showing how it's used