O ThreadWeaver que fará a sua aparição no KDE 4.0, espera resolver boa parte dos problemas enfrentados pelos desenvolvedores (e em certa medida pelos usuários) no uso de threads e/ou múltiplos cores.
Estudos de caso:
ThreadWeaver provides a solution to a number (but not all) multithreading problems. Let’s have a look:
- How do you implement a single operation that takes a lot of CPU power,
but is hard to handle in chunks (Example: scale an image) when you want
your GUI to remain responsive? Encapsulate the operation in a class
object derived from Job instead of a method and put it in the static instance (Weaver::instance). Connect to the Job’s done() signal to receive a notification when it is completed.
- How do you implement a CPU intensive operation while time-critical
operations are executed (load and decode an MP3 while another one is
fed to the audio device): Implement both the file loading operation and
the play operation in a job and queue both at the same time. There is
also a synchronous sleep method in the Job class if a job needs to be delayed for a number of millieconds after it is taken over by a thread.
- How do you implement many small operations where the cost of each
individual one is hard to estimate (loading two hundred icons from an
NFS drive): Create a common or a number of specialized job classes.
Queue all of them. Either connect to the individual done signal to
process every item when it has been finished or connect to
Weaver::jobsDone() to receive a notification when all of the jobs are
- How do you implement an operation with complex control flow and
dependencies in the execution order (load, parse and display an HTML
document with embedded media): Create jobs for the individual steps you
need to perform. Try to split the whole operation in as many
independent, parallelizable parts as possible. Now declare the
execution dependencies. A job will only be executed when all jobs it
depends on are finished. This way every individual operation will be
executed as soon as it becomes possible. Connect to the final jobs
done() signal to be notified when all parts of the whole operations
have been executed. If necessary (if there is more than one final
object), create a dummy job object that depends on all of them to have
one central end point of execution.
As you can see, ThreadWeaver
can provide solutions for simple, but also for complex cases. For an
example on how job dependencies can be modeled and used to create
elegant, streamlined solutions for control flow modeling, see the
Simple Multithreaded Image Viewer (SMIV) example in the Tests directory.