I'm writing my first proper useful piece of software. Part of it will involve the user viewing an image, and choosing to accept or reject it. Doing this will cause the image to be saved to an accepted or rejected folder, and possibly rotated and/or resized.
At the moment, my rotate/resize/save operation is pausing execution of my program, but I'd like it to happen in the background so the next image is displayed instantly.
Is the only way to do this in Qt to process the image in a separate thread, or is there another way? I'm still getting my head round C++ and Qt, so I don't want to confuse myself by diving into a new field!
Unlike processes, threads share data and information. We can create more than one thread by using just one system call. To further simplify things, thread management requires few or even no system calls because we don't need extra mechanisms such as IPC to maintain communication between threads.
Threads use the memory of the process they belong to. Inter-process communication is slow as processes have different memory addresses. Inter-thread communication can be faster than inter-process communication because threads of the same process share memory with the process they belong to.
A thread is an execution unit that has its own program counter, a stack and a set of registers that reside in a process. Threads can't exist outside any process.
You'd prefer multiple threads over multiple processes for two reasons: Inter-thread communication (sharing data etc.) is significantly simpler to program than inter-process communication. Context switches between threads are faster than between processes.
Qt has thread support. You might find this example application interesting since it's somewhat similar to what you describe.
Also, here is the full Qt thread documentation.
these kind of tasks are perfectly suited for threads. still, you should first do a 'normal' function that does it, and when it works add a thread that reads a queue and calls the same processing function.
Qt has a lot of tools to help you on this, mainly the fact that most of the containers are thread-safe, and also a couple of threading algorithms (like map-reduce). still, first try it synchronously.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With