关于C#:为什么is_lock_free是成员函数?

Why is is_lock_free a member function?

is_lock_free为什么需要一个实例(它是成员函数)的原因是什么?为什么不使用该类型的元函数或静态constexpr成员函数?

我正在寻找为什么需要这样做的实际例子。


该标准允许类型有时是无锁的。

section 29.4 Lock-free property

The ATOMIC_..._LOCK_FREE macros indicate the lock-free property of the
corresponding atomic types, with the signed and unsigned variants
grouped together. The properties also apply to the corresponding
(partial) specializations of the atomic template. A value of 0
indicates that the types are never lock-free. A value of 1 indicates
that the types are sometimes lock-free. A value of 2 indicates that
the types are always lock-free.

C原子论文n2427指出了其背后的原因:

... The proposal provides run-time lock-free query functions rather
than compile-time constants because subsequent implementations of a
platform may upgrade locking operations with lock-free operations, so
it is common for systems to abstract such facilities behind dynamic
libraries, and we wish to leave that possiblility open. Furthermore,
we recommend that implementations without hardware atomic support use
that technique. ...

而且(正如杰西·古德(Jesse Good)所指出的那样):

The proposal provides lock-free query functions on individual objects rather than whole types to permit unavoidably misaligned atomic variables without penalizing the performance of aligned atomic variables