-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tf: allow TF_MAKE_STATIC_DATA to provide const access
TF_MAKE_STATIC_DATA can now be used to define a TfStaticData instance that only allows const access to the underlying data. The data itself is non-const so that initialization code can be used in a straightforward manner. This is particularly useful for copy-on-write types where seemingly innocuous access (e.g. range-for iteration) can trigger detachment. Consider the following example, TF_MAKE_STATIC_DATA(VtIntArray, _array) { *_array= { /* ... */ }; } void Func() { WorkParallelForN(_array->size(), [](size_t f, size_t l) { for (; f != l; ++f) { int x = (*_array)[f]; // invokes non-const operator[] // ... } } } This code attempts to read _array in parallel but, because VtIntArray is copy-on-write and TfStaticData::operator* returns a non-const reference, multiple threads may race to detach if the array has been shared previously. TF_MAKE_STATIC_DATA(const VtIntArray, _array) solves this issue. (Internal change: 2237275)
- Loading branch information
Showing
2 changed files
with
28 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters