- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个问题,我需要能够自动更新两个 uint64_t
的同时。以原子方式编写它们中的每一个是很容易的(例如,有两个 std::atomic<uint64_t>
的),但这仍然会导致更新一个而另一个不更新的情况。使用锁和互斥锁也很容易实现。
但我想以原子方式编写,没有任何类型的锁,这样我仍然可以拥有类型为 uint64_t
的成员变量。这样就不会锁定读取。这是因为我的用例涉及读取它们很多很多次,但很少写入它们(~读取 1x/ms,写入 1x/5 分钟)。可能吗?如果是这样,如何?
最佳答案
对于 std::atomic
标准说(强调我的)
The primary
std::atomic
template may be instantiated with any TriviallyCopyable type T:struct Counters { int a; int b; }; // user-defined trivially-copyable type
std::atomic<Counters> cnt; // specialization for the user-defined type
所以你可以只创建一个结构 2 uint64_t
像这样
struct atomic128 {
uint64_t a1, a2;
};
这是简单可复制的(很容易用 std::is_trivially_copyable
确认),然后使用 std::atomic<atomic128>
.你会得到一个错误 if std::atomic<type>
is not trivially copyable
这样编译器将自动使用无锁更新机制(如果可用)。无需执行任何特殊操作,如有必要,只需使用以下任一方法检查即可
All atomic types except for std::atomic_flag may be implemented using mutexes or other locking operations, rather than using the lock-free atomic CPU instructions. Atomic types are also allowed to be sometimes lock-free: for example, if only some subarchitectures support lock-free atomic access for a given type (such as the CMPXCHG16B instruction on x86-64), whether atomics are lock-free may not be known until runtime.
Here's a demo在编译器资源管理器上。如您所见lock cmpxchg16b
被发出,尽管 GCC 7 和更高版本只会调用 __atomic_store_16
哪个internally use cmpxchg16b
if it's available
在某些平台上 long double
是 128 位类型或被填充为 128 位,因此 std::atomic<long double>
可能是另一种解决方案,但当然你需要先检查它的大小以及它是否是无锁的
另一种选择是 Boost.Atomic .它还具有宏 BOOST_ATOMIC_INT128_LOCK_FREE
and BOOST_ATOMIC_LONG_DOUBLE_LOCK_FREE
检查
在某些 CPU 上,128 位 SSE 操作也是原子操作,不幸的是,没有办法检查您是否可以使用它
关于c++ - 你能自动写两个 uint64_t 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19394946/
根据在线文档,这些固定宽度整数类型之间存在差异。对于 int*_t,我们将宽度固定为 * 的值。然而对于其他两种类型,描述中使用形容词最快和最小来请求底层数据模型提供的最快或最小实例。 “最快”或“最
我是 Python 注释的新手(类型提示)。我注意到 pyi 中的许多类定义文件继承到 Generic[_T] , 和 _T = TypeVar('_T') . 我很困惑,_T 是什么意思?这里的意思
这个问题在这里已经有了答案: How to use a variable inside a _T wrapper? (3 个答案) 关闭 7 年前。 我有以下代码: CString port = m
要包含 _T() 宏,我应该包含什么文件?它转换我认为的文本文字。我以为它是 windows.h,但我已经包含了它。 令人惊讶的是,我无法在 Google 上找到答案。 最佳答案 我在主题 Unico
类型的后缀 _t 由 POSIX 保留,但是如果我在自己的命名空间中使用 _t 后缀定义自己的类型怎么办? 最佳答案 我同意 user6366161 的 answer,其中说“C 对 namespac
我知道 size_t 有 _t 后缀,因为它的别名/typedef。但是我不明白为什么 char16_t, char32_t 和 wchar_t 包含 _t 后缀。 最佳答案 对于 wchar_t :
我想让这个字符串的主机名部分可变..目前,它只修复了这个 URL: _T(" --url=http://www.myurl.com/ --out=c:\\current.png"); 我想做这样的东西
这个问题在这里已经有了答案: convert string to _T in cpp (6 个答案) 关闭 7 年前。 string pagexx = "http://website.com/" +
我有一个注册为 COM 对象的 .net 库,当在 C++ 项目中导入 .tlb 文件时,我得到这样的方法声明 virtual HRESULT __stdcall GetBid ( /*[
我现在遇到了很多 Unicode 问题。据我了解,TCHAR 被定义为 wchar_t 或 char,具体取决于 _UNICODE 是否在某处定义,并且还有各种其他功能可以帮助解决这个问题。显然 _T
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我正在尝试使用 _t() 方法翻译一个 DataObject。 我一直在 Pages 上使用它没有问题,但它似乎不适用于数据对象。 class SliderItem extends DataObjec
关于保留 _t 结尾名称的规则是否也适用于作用域名称(例如,在 namespace 或类中定义的类型和类型定义),还是仅适用于全局 namespace 中的类型和类型定义?标准 C/C++ 库或 PO
我确定以前有人问过这个问题,但我无法搜索到文本。如果有人可以解释它们,请给我推荐一篇文章,或者给我正确的搜索查询,我将不胜感激。 谢谢。 最佳答案 这只是一种预感,但看看 Wikipedia C++1
我有一个 UNICODE 应用程序,我们使用 _T(x) 定义如下。 #if defined(_UNICODE) #define _T(x) L ##x #else #define _T(x) x #
我一直想知道是否存在任何命名约定,例如何时对类型使用 ALLCAPS 以及何时附加 _t(以及何时不使用任何东西?)。我知道以前 K&R 发布了各种关于如何使用 C 的文档,但我找不到任何相关内容。
这似乎是一个简单的问题,但我无法通过 Stack Overflow 搜索或 Google 找到它。类型后跟 _t 是什么意思?比如 int_t anInt; 我在 C 代码中经常看到它与硬件密切相关—
C++ 有时使用后缀 _type关于类型定义(例如 std::vector::value_type ),有时_t (例如 std::size_t ),或者没有后缀(普通类,还有像 std::strin
字符串中的“T”代表什么。例如 _T("Hello")。我在需要 unicode 支持的项目中看到了这一点。它实际上告诉处理器什么 最佳答案 _T 代表“文本”。当且仅当您使用 Unicode 支持编
我的代码可以根据 C++ 类型识别您需要使用的 GL 类型。我想制作它的 _t 版本(如 std::decay_t 或 std::enable_if_t)但公开 int常量值 template st
我是一名优秀的程序员,十分优秀!