qoperatingsystemversion_win: fix thread race

If two threads call the function at the same time before the static is initialized
one of them may end up with a half-written object.

Amends 3fe89eec61

Pick-to: 6.2
Change-Id: Ie08970f9ee283fd75292a8b44a1fca89de4b04eb
Reviewed-by: Yuhang Zhao <2546789017@qq.com>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Mårten Nordheim 2021-11-08 14:05:41 +01:00
parent 2010df52d4
commit ac7e94090f

View File

@ -110,14 +110,15 @@ OSVERSIONINFOEX qWindowsVersionInfo()
QOperatingSystemVersion QOperatingSystemVersion::current()
{
static QOperatingSystemVersion v;
if (v.m_os == Unknown) {
static QOperatingSystemVersion v = [](){
QOperatingSystemVersion v;
v.m_os = currentType();
const OSVERSIONINFOEX osv = qWindowsVersionInfo();
v.m_major = osv.dwMajorVersion;
v.m_minor = osv.dwMinorVersion;
v.m_micro = osv.dwBuildNumber;
}
return v;
}();
return v;
}