Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a separate process or thread in Qt

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!

like image 272
Skilldrick Avatar asked May 28 '09 22:05

Skilldrick


People also ask

When would you use a thread instead of a process?

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.

Which is faster process or thread?

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.

Can a thread run without a process?

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.

Why are multiple threads usually preferred to multiple processes?

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.


2 Answers

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.

like image 78
Naaff Avatar answered Sep 29 '22 10:09

Naaff


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.

like image 30
Javier Avatar answered Sep 29 '22 12:09

Javier