"Beautifying"宏
"Beautifying" macro
本文关键字:Beautifying 更新时间:2023-10-16
在观看Herb Sutter关于C++及其他语言中原子论的演讲时,我瞥见了他关于一种易于使用的锁定/解锁机制的想法,这种机制可能会也可能不会出现在未来的语言标准中。
机制看起来像:
atomic{
// code here
}
由于不想等待未来的标准,我自己尝试实现了这个标准,我想到的是:
#define CONCAT_IMPL(A, B) A ## B
#define CONCAT(A, B) CONCAT_IMPL(A, B)
# define atomic(a) {
static_assert(std::is_same<decltype(a), std::mutex>::value,"Argument must be of type std::mutex !");
struct CONCAT(atomic_impl_, __LINE__)
{
std::function<void()> func;
std::mutex* impl;
CONCAT(atomic_impl_, __LINE__)(std::mutex& b)
{
impl = &b;
impl->lock();
}
CONCAT(~atomic_impl_, __LINE__)()
{
func();
impl->unlock();
}
} CONCAT(atomic_impl_var_, __LINE__)(a);
CONCAT(atomic_impl_var_, __LINE__).func = [&]()
和用法:
std::mutex mut;
atomic(mut){
// code here
};}
显然,问题在于}我想删除它。
这有可能吗?
您可以使用在if
语句中定义变量的技巧来实现这一点。
template <typename M>
struct atomic_guard_ {
explicit atomic_guard_(M& m) : lock(m) {}
atomic_guard_(M const&) =delete; // Since we unfortunately have to use uniform
atomic_guard_(M&&) =delete; // initialization, make it at least a little safe
operator bool() const { return false; }
std::lock_guard<M> lock;
};
#define atomic(m)
if (atomic_guard_<std::decay<decltype(m)>::type> _{m}) {} else
int main()
{
std::mutex m;
atomic(m) {
std::cout << "an";
}
atomic(m) // this works too, but I think this is ok
std::cout << "bn";
}
使用lock_guard
。如果你想涉及一个宏,我想你可以,它将节省你选择变量名的时间,并避免意外创建临时变量而不是变量的(令人惊讶的容易)拼写错误。
更简单的版本将使用for
和std::unique_lock
:
#include <iostream>
#include <type_traits>
#include <mutex>
#define atomic(a)
for(auto lock=std::unique_lock<std::decay<decltype(a)>::type>(a); lock; lock.unlock())
int main()
{
std::mutex m;
atomic(m) {
std::cout << "Hello, world!" << std::endl;
}
}
相关文章: