我有一个代码运行在一些安全关键汽车模块。以下是对代码的粗略估计:
下面的代码是模块“主模块”的一部分,该模块拥有易失性变量/数组"x_ast“
Main Module.c
//The structure x contains the major data that needs to be stored in case of a crash event. i.e. a real car crash
// x contains data such a vehicle speed, environment data, sensor data, CAN related data,etc. B
我有一个有点像这样的服务器:
class Server {
private WorkingThing worker;
public void init() {
runInNewThread({
// this will take about a minute
worker = new WorkingThing();
});
}
public Response handleRequest(Request req) {
if (worker == null
考虑以下示例: #include <iostream>
class C {
int intArray[2] { 1, 2 };
int *firstElementPt;
public:
int getFirstElement() volatile {
firstElementPt = intArray;
return *firstElementPt;
};
};
int main()
{
volatile C c;
std::cout << c
我想写一个非常简单的驱动程序来初始化,在微控制器上从一些外围设备写入/读取,就像我在C中所做的那样。我用GPIO外围设备来初始化、写入和读取。
GPIOA : GPIO_Register with
Volatile,
Address => System'To_Address (GPIOA_Base);
pragma Import (Ada, GPIOA);
如果我声明一个列表来访问所有的GPIOs:
type GPIO_Register_ptr is access all GPIO_Register with volatile;
Gpio_List_Poi
我有一条线
write a = 0
write a = 1
write volatile flag = 1
在第二条线上
read volatile flag // This always happens after I write volatile flag in thread 1
read a
是否可以进行重新排序,以便在第二个线程中看到read a返回0?
如果没有,能请人详细解释原因吗?
我之所以问这个问题,是因为我对JLS的定义感到困惑:
在每个线程t执行的所有线程间操作中,t的程序顺序是一个总顺序,它反映了根据t的线程内语义执行这些操作的顺序。
在这种情况下,似乎允许重新排
我需要在公式中使用indirect函数,因为它引用了可以删除的工作表2。我不想在不需要的时候触发间接函数,因为它的易变性。所以我写了这个公式:
=IF(abc=1;"";INDIRECT("Sheet 2!zzz"))
我得到了我想要的结果("“或zzz值)。但它似乎总是不稳定的!我使用这个宏来测试它(可以在上找到):
Option Base 1
Option Compare Text
'-----------------------
Public jCalcSeq As Long ''' calc
我知道这意味着对数组的引用是易失性的,而不是数组中的项,如果你声明一个数组volatile。
我正在学习互斥算法,所以我写了一些测试代码:
public class MutualExclusion {
static final int N = 10;
static final int M = 100000;
volatile static int count = 0;
public static void main(String[] args) {
Thread[] threads = new Thread[N];
for (
以下代码:
while (x == 1) { ... }
可能被优化为
while (true) { ... }
如果x只在另一个线程中分配。见。答案是通过将x设置为volatile来解决这个问题。
然而,根据这三位贡献者的说法,这似乎并不是正确的方法(综合声誉超过100万:)
Hans :“千万不要假设它在多线程场景中很有用。”
Marc Gravell :“易失性不是我通常用来保证行为的语义”
Eric :“我不鼓励你做一个不稳定的领域。”
假设一个Backgroundworker像对待任何其他多线程一样(而不是有一些防止优化的内置机制),那么如何防止糟糕的优化呢?
我写的一些低级代码有一个问题,我需要使用对象作为易失性,但这并不是必须的,所以我希望类型被声明为易失性(出于可重用性的原因)。但是,我可以定义指向结构的限定变量的指针,如以下部分所述。
struct x {
int bar;
};
struct x foobar;
...
volatile struct x *foo = &foobar;
现在foo实际上是指向以下类型的对象的指针:
volatile struct x {
volatile int x;
};
因为易失性适用于所有结构成员。现在我的问题是,当一个对象包含指向另一个对象的指针时,如何应用易失性?
struct