What are the thread safety requirements of HSTRING and BSTR?
They do not have thread affinity. The post What are the thread safety requirements of HSTRING and BSTR? appeared first on The Old New Thing.

Among the proliferation of string types are the HSTRING
(represented in C++/WinRT as winrt::
and in C++/CX as String^
) and the BSTR
. What are the threading rules for these string types?
These string types are not COM objects, so the restrictions on COM objects do not apply. These are just blocks of memory that have freestanding functions for manipulating them (such as SysAllocString
and SysFreeString
for BSTR
; WindowsCreateString
and WindowsDeleteString
for HSTRING
). You are welcome to call any method from any thread.
The rules for BSTR
are that read operations (like SysGetStringLen
) can operate concurrently. But no read operations can operate concurrently with a write operation, so you cannot do a SysGetStringLen
at the same time as a SysReAllocString
, for example, and you can’t modify the string contents on one thread while reading them from another.
Windows Runtime HSTRING
s are immutable, so there are no write operations. This makes the rules simpler: Once you create an HSTRING
(until you destroy it), all operations are thread-safe.
The post What are the thread safety requirements of HSTRING
and BSTR
? appeared first on The Old New Thing.