使用宏创建自定义块类型
Create a custom block type using macros
在avr gcc中,您可以执行以下操作:
ATOMIC_CODE{
cout << "Here I can do stuff that is very time sensitiven";
}
不幸的是,这是一个#define,它使用了一个特殊的gccatribute,我想避免它。
因此,一个变通方法是:
void enableInterrupts(){ std::cout << "Interupts Enabledn"; }
void disableInterrupts() { std::cout << "Interupts Disabledn"; }
class Scoped{
void (*cleanup)();
bool incremented;
public:
Scoped(void (*clean)(),void (*before)()) : cleanup(clean),incremented(false){
before();
}
~Scoped(){
cleanup();
}
void operator ++(){
incremented = true;
}
bool operator!(){
return !incremented;
}
};
#define ATOMIC for (Scoped x(&enableInterrupts,&disableInterrupts); !x; ++x)
//Later in main.cpp
ATOMIC{
/*do stuff*/
std::cout << "This is atomic coden";
}
唯一的问题是它依赖于立即调用析构函数(我不确定这是一个可靠的方法)。
那么,是否可以保证析构函数会立即被调用,或者编译器可以在任何时候对对象进行析构函数?
是的,保证会立即调用析构函数。
https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
您正在考虑的语言可以进行懒惰标记和清除垃圾收集。(好吧,也许你不是)
相关文章:
- 使用QJsEngine在Qt中注册自定义类型
- 使用模板类的自定义列表类型中的内存泄漏
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 如何使自定义类型在unordered_map中用作键
- 自定义引用类型
- 具有自定义构造函数 (C++) 的类型的动态数组分配
- Qt5 远程对象 + 自定义类型,但不在 POD 中
- 如何定义自定义生成配置类型,其中通常是.exe的目标改为 DLL
- 标准::原子中的自定义类型
- 在自定义 std::vector-like 容器中处理指针和非指针模板类型的最佳方法是什么?
- 在Qt中注册自定义元类型的别名类型
- 如何在C++中允许成员函数的自定义返回类型进行类型擦除?
- 如何使用自定义类型声明Arduino数组?
- 特征中的自定义标量类型
- 在点云库 (PCL) 中使用自定义点类型的问题
- 为自定义打印调试实现传递任何类型的变量
- 使用自定义访问者时具有自定义类型的提升变体失败(源自 boost::static_visitor)
- 错误 - 自定义数据类型作为有效负载,带有提升::几何
- 将成员函数的返回类型引用到C++中的自定义类