The starting point for the thread. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. June 23, at When this signal is emitted, the event loop has already stopped running. Is there a way to invoke a private slot without a connection? You say that one really does not often need to subclass QThread.
Meanwhile, the thread that called the signal will acquire the semaphore in order to wait until the event is processed. It is the destructor of QMetaCallEvent which will release the semaphore. This is good because the event will be deleted right after it is delivered i. A BlockingQueuedConnection can be useful to do thread communication when you want to invoke a function in another thread and wait for the answer before it is finished.
However, it must be done with care. Obviously, if you connect two objects using BlockingQueuedConnection living on the same thread, you will deadlock immediately. You are sending an event to the sender's own thread and then are locking the thread waiting for the event to be processed. Since the thread is blocked, the event will never be processed and the thread will be blocked forever. Qt detects this at run time and prints a warning, but does not attempt to fix the problem for you. It has been suggested that Qt could then just do a normal DirectConnection if both objects are in the same thread.
But we choose not to because BlockingQueuedConnection is something that can only be used if you know what you are doing: You must know from which thread to what other thread the event will be sent.
The real danger is that you must keep your design such that if in your application, you do a BlockingQueuedConnection from thread A to thread B, thread B must never wait for thread A, or you will have a deadlock again. When emitting the signal or calling QMetaObject:: A problem will typically appear when you need to terminate a thread using a BlockingQueuedConnection , for example in this pseudo code:.
You cannot just call wait here because the child thread might have already emitted, or is about to emit the signal that will wait for the parent thread, which won't go back to its event loop. All the thread cleanup information transfer must only happen with events posted between threads, without using wait.
A better way to do it would be:. Thank you very much. I am a bit confused now about movetothread while I know it is used later in the thread for events and I need to get help by reading a source code which has correct implementation. As I remember in the documents ,:: Start should not be called from the thread itself!!!
Start should not be called from the thread itself! What do you mean with that? It's supposed to be called from the thread your thread object is living in. Calling a custom method foo which in turns calls start is perfectly fine. Start has been called from thread subclass and not the thread that object lives in.
It is not impossible to subclass QThread and use it that way, but it is no longer the recommended way to do things. I won't say you should always use the create-a-worker-QObject-and-move-it-to-a-vanilla-QThread method, but in general, yes, that is the way to go.
Especially if you plan to start using signals and slots. What's the example you're talking about? And "from the thread subclass" doesn't mean much; as I said, adding a method which in turn calls start and calling that method is fine and I doubt start is getting called from inside run.
That processImage method gets called from the right thread in the example, the GUI thread, which is where the RenderThread object is living , so it's safe to use that way.
QThread with signals and slots This topic has been deleted. Only users with topic management privileges can see it. I've got the following code: MyClass ; private slots: Does anyone know why this is? Reply Quote 0 0 Replies Last reply.
I've tried doing this: You can use worker objects by moving them to the thread using QObject:: The code inside the Worker's slot would then execute in a separate thread.
However, you are free to connect the Worker's slots to any signal, from any object, in any thread. It is safe to connect signals and slots across different threads, thanks to a mechanism called queued connections. Another way to make code run in a separate thread, is to subclass QThread and reimplement run.
In that example, the thread will exit after the run function has returned. There will not be any event loop running in the thread unless you call exec. It is important to remember that a QThread object usually lives in the thread where it was created, not in the thread that it manages. This oft-overlooked detail means that a QThread's slots will be executed in the context of its home thread, not in the context of the thread it is managing.
For this reason, implementing new slots in a QThread subclass is error-prone and discouraged. It is not possible to change the thread affinity of GUI objects; they must remain in the main thread. QThread will notifiy you via a signal when the thread is started , finished , and terminated , or you can use isFinished and isRunning to query the state of the thread. You can stop the thread by calling exit or quit. In extreme cases, you may want to forcibly terminate an executing thread.
However, doing so is dangerous and discouraged. Please read the documentation for terminate and setTerminationEnabled for detailed information. Use wait to block the calling thread, until the other thread has finished execution or until a specified time has passed. The static functions currentThreadId and currentThread return identifiers for the currently executing thread.
The former returns a platform specific ID for the thread; the latter returns a QThread pointer. To choose the name that your thread will be given as identified by the command ps -L on Linux, for example , you can call setObjectName before starting the thread. If you don't call setObjectName , the name given to your thread will be the class name of the runtime type of your thread object for example, "RenderThread" in the case of the Mandelbrot Example , as that is the name of the QThread subclass.
Note that this is currently not available with release builds on Windows. QThread also provides static, platform independent sleep functions: Instead of wait , consider listening for the finished signal. Instead of the sleep functions, consider using QTimer. Constructs a new QThread to manage a new thread. The parent takes ownership of the QThread. The thread does not begin executing until start is called. Note that deleting a QThread object will not stop the execution of the thread it manages.
Deleting a running QThread i. Wait for the finished signal before deleting the QThread. Returns a pointer to a QThread which manages the currently executing thread. The handle returned by this function is used for internal purposes and should not be used in any application code.
On Windows, the returned value is a pseudo-handle for the current thread. It can't be used for numerical comparison. Enters the event loop and waits until exit is called, returning the value that was passed to exit.
The value returned is 0 if exit is called via quit. See also quit and exit. After calling this function, the thread leaves the event loop and returns from the call to QEventLoop:: Note that unlike the C library function of the same name, this function does return to the caller -- it is event processing that stops.
If the eventloop in QThread:: See also quit and QEventLoop. See also started and terminated. Returns the ideal number of threads that can be run on the system.