c++ - Using observer pattern in the context of Qt signals/slots -
c++ - Using observer pattern in the context of Qt signals/slots -
i trying create design decisions algorithm working on. think want utilize signals , slots implement observer pattern, not sure of few things.
here algorithm working towards:
1.) load tiles of image big file 1a.) re-create entire file new location 2.) process tiles loaded 3.) if re-create has been created, re-create resulting info new file so envision having class functions loadalltiles() emit signals tell processtile() tile ready processed, while moving on load next tile.
processtile() perform calculations, , when complete, signal writeresults() new set of results info ready written. writeresults() verify copying complete, , start writing output data.
does sound reasonable? there way create loadalltiles() load in tile, pass info somehow processtile() , maintain going , load next tile? thinking maybe setting list of sort store tiles ready processed, , list result tiles ready written disk. guess disadvantage there have somehow maintain lists in tact, multiple threads arent trying add/remove items list.
thanks insight.
it's not clear in question, seems want split work several threads, processing of tiles can begin before finishing loading entire set.
consider multithreaded processing pipeline architecture. assign 1 thread per task (loading, copying, processing), , pass around tiles between tasks via producer-consumer queues (aka blockingqueue). more precise, pass around pointers (or shared pointers) tiles avoid needless copying.
there doesn't seem ready-made thread-safe blockingqueue class in qt, can roll-up own using qqueue, qwaitcondition, , qmutex. here sources of inspiration:
while there isn't ready-made blockingqueue within qt, seems using signals & slots qt::queuedconnection alternative may serve same purpose. qt blog article makes such utilize of signals , slots.
you may want combine pipeline approach memory pool or free list of tiles, allocated tiles recycled in pipeline.
here's conceptual sketch of pipeline:
tilepool -> tileloader -> pcq -> tileprocessor -> pcq -> tilesaver -\ ^ | \----------------------------------------------------------------/ where pcq represents producer-consumer queue.
to exploit more parallelism, can seek thread pools @ each stage.
you can consider checking out intel's threading building blocks. haven't tried myself. aware of gpl licence open source version.
c++ qt design-patterns
Comments
Post a Comment