2

When using a QML WorkerScript, are the requests (sent via postMessage()) queued (and executed on a single handler thread) or is there the possibility/danger that two successive postMessage() will be executed on two threads concurrently?

1 Answer 1

5

Short answer

All WorkerScript should execute in the same thread.

Not so short answer

All WorkerScript in a QQmlEngine should execute in the same thread.

Long answer

When you create a WorkerScript in QML you instantiate the QQuickWorkerScript C++ class. This class uses the QQuickWorkerScriptEngine class to handles all the thready things.

Now, if you look at QQuickWorkerScript::engine() and QQmlEnginePrivate::getWorkerScriptEngine() you will see that all WorkerScript objects will share the same QQuickWorkerScriptEngine instance as long as they share the same QQmlEngine.

Also QQuickWorkerScriptEngine is a QThread (public inheritance) and contains a member variable named d of type QQuickWorkerScriptEnginePrivate *. d is running in the thread handled by QQuickWorkerScriptEngine (see d->moveToThread(this) in QQuickWorkerScriptEngine constructor). And it is this d that will effectively run the asynchronous work in QQuickWorkerScriptEnginePrivate::event().

PS

This kind of contradicts Qt documentation that states:

Use WorkerScript to run operations in a new thread.

Which might make you think that each WorkerScript is a new thread.

Sign up to request clarification or add additional context in comments.

1 Comment

"Which might make you think that each WorkerScript is a new thread" Yeah, that's what has prompted me to ask the question. For the future, a thread pool size for WorkerScript would be nice for cases where more than one executor is desirable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.