Skip to content
Verified Commit af5a6d38 authored by Daniel Vrátil's avatar Daniel Vrátil 🤖
Browse files

Fix a curious race condition when initializing AkThread-derived classes

When a class derived from AkThread is constructed, the AkThread
constructor is called first. In this constructor AkThread starts
a new thread and moves the object into that thread. Then it uses
QMetaObject::invokeMethod to schedule invoking the virtual
this->init() method on the new thread. If the new thread is very
fast to start, there's a chance that it will run the init() method
before the main thread manages to reach the derived class constructor
and update the vtable. In such situation, the runtime will invoke
AkThread::init() instead of Derived::init(), and it may also race
with the code in Derived constructor being executed on the main
thread.

The only reliable solution for this is to request invoking the init()
method only after the entire Derived object has been constructed, that
is outside of the constructor call chain. As such, this change
introduces AkThread::create() static template method which constructs
AkThread-derived object and then invokes the init() method.
parent a3b3f3bd
Pipeline #375713 passed with stage
in 26 minutes and 51 seconds
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment