Warning, /frameworks/threadweaver/docs/use-cases.md is written in an unsupported language. File is not indexed.
0001 Use Cases {#usecases}
0002 =========
0003
0004 ThreadWeaver provides a solution to a number (but not all)
0005 multithreading problems. Let's have a look:
0006
0007 - How do you implement a single operation that takes a lot of CPU power, but is
0008 hard to handle in chunks (Example: scale an image) when you want your GUI to
0009 remain responsive? Encapsulate the operation in a class object derived from
0010 Job instead of a method and put it in the static instance (Weaver::instance).
0011 Connect to the Job's done() signal to receive a notification when it is
0012 completed.
0013 - How do you implement a CPU intensive operation while time-critical operations
0014 are executed (load and decode an MP3 while another one is fed to the audio
0015 device): Implement both the file loading operation and the play operation in
0016 a job and queue both at the same time. There is also a synchronous sleep
0017 method in the Job class if a job needs to be delayed for a number of
0018 milliseconds after it is taken over by a thread.
0019 - How do you implement many small operations where the cost of each individual
0020 one is hard to estimate (loading two hundred icons from an NFS drive): Create
0021 a common or a number of specialized job classes. Queue all of them. Either
0022 connect to the individual done signal to process every item when it has been
0023 finished or connect to Weaver::jobsDone() to receive a notification when all
0024 of the jobs are done.
0025 - How do you implement an operation with complex control flow and dependencies
0026 in the execution order (load, parse and display an HTML document with
0027 embedded media): Create jobs for the individual steps you need to perform.
0028 Try to split the whole operation in as many independent, parallelizable parts
0029 as possible. Now declare the execution dependencies. A job will only be
0030 executed when all jobs it depends on are finished. This way every individual
0031 operation will be executed as soon as it becomes possible. Connect to the
0032 final jobs done() signal to be notified when all parts of the whole
0033 operations have been executed. If necessary (if there is more than one final
0034 object), create a dummy job object that depends on all of them to have one
0035 central end point of execution.
0036
0037 As you can see, ThreadWeaver can provide solutions for simple, but
0038 also for complex cases. For an example on how job dependencies can be
0039 modeled and used to create elegant, streamlined solutions for control
0040 flow modeling, see the Simple Multithreaded Image Viewer (SMIV) example
0041 in the Tests directory.
0042
0043 ThreadWeaver focuses on operations that can be implemented in
0044 Jobs. To create solutions where a thread is supposed to constantly run
0045 to perform an ongoing operation that, for example, spans the whole
0046 application live, it has to be verified that it is the right
0047 approach. It is possible to add very long or neverending operations to
0048 the queue, but it will constantly bind a thread to that operation. It
0049 might still make sense to use ThreadWeaver, as the creation, handling
0050 and destruction of the threads is already taken care of, but the minimum
0051 inventory size (thread count) should be increased accordingly to provide
0052 for enough threads to process the other jobs.
0053