简体   繁体   English

目标C,将Java中的volatile转换为Objective C的最佳方法是什么?

[英]Objective C, What is the best way to convert volatile in Java into Objective C?

I am trying to convert Java code into Objective C code. 我正在尝试将Java代码转换为Objective C代码。 And the java code contains variables defined as volatile. 并且java代码包含定义为volatile的变量。 I looked online and "volatile" usage in java as follwing 我在网上查看了java中的“volatile”用法,如下所示

 Essentially, volatile is used to indicate that a variable's value will be modified by different threads.

So, If I was going to set variables as volatile in Objective C because the variables are going to be accessed by different threads then I don't need to set those variables as volatile because I can just set those variables as atomic? 所以,如果我要在Objective C中将变量设置为volatile,因为变量将由不同的线程访问,那么我不需要将这些变量设置为volatile,因为我可以将这些变量设置为atomic?

The volatile keyword exists in Objective-C as well. volatile关键字也存在于Objective-C中。 You can use it. 你可以使用它。

This is because Objective-C is a superset of C. 这是因为Objective-C是C的超集。

Declaring the properties as atomic will not correct what volatile was meant to do. 将属性声明为atomic属性将无法纠正volatile意图。 volatile effectively tells the compiler not to optimize away checks done on that variable, because it may have changed when the compiler expected it to stay the same. volatile有效地告诉编译器不要优化对该变量进行的检查,因为当编译器期望它保持不变时它可能已经改变了。

The simplest example is this. 最简单的例子就是这样。 Say we have a global variable declared as: 假设我们有一个全局变量声明为:

int packetsReceived = 0;

And it's later used like this: 它后来使用如下:

packetsRecieved = 0;

while (packetsRecieved < 10){
    //Wait for more packets
}

processPackets();

We will never get through that loop, because the compiler will say "Hey, packetsRecieved is never modified in that loop, therefore it will run infinitely." 我们永远不会通过那个循环,因为编译器会说“嘿,在这个循环中从不修改packetsRecieved ,因此它将无限运行”。 As a result, it will just make it a straight infinite loop so it can avoid having to check every time. 因此,它只会使它成为一个直的无限循环,因此它可以避免每次都检查。

If we instead had declared the variable as: 如果我们改为将变量声明为:

volatile int packetsRecieved;

We are telling the compiler that this variable may change at any time, even when it looks like it should stay the same. 我们告诉编译器这个变量可能随时改变,即使它看起来应该保持不变。 So in our example, the machine code generated by the compiler will still have a check on the condition, and our program will work as expected. 因此在我们的示例中,编译器生成的机器代码仍将检查条件,并且我们的程序将按预期工作。

No, atomic and volatile are not the same thing. 不, atomicvolatile物不是一回事。

atomic (in a property declaration) means that the getter/setter will ensure that a whole value is get/set, regardless of what other threads might be doing simultaneously. atomic (在属性声明中)意味着getter / setter将确保获取/设置整个值,而不管其他线程可能同时执行什么操作。

volatile is an indicator to the compiler that a variable can be modified by other means (other threads, memory-mapped IO devices), so it should not optimize out (seemingly) unnecessary loads/stores of that variable. volatile是编译器的一个指示器,可以通过其他方式(其他线程,内存映射的IO设备)修改变量,因此它不应该优化(看似)该变量的不必要的加载/存储。 That's similar to what it means in Java, although Java adds some guarantees about memory barriers and ordering of reads and writes that C (and Objective-C) don't provide. 这与Java中的含义相似,尽管Java增加了一些关于内存障碍和C(和Objective-C)不提供的读写顺序的保证。 In particular, in C, simply declaring a variable volatile is not sufficient to safely use it from multiple threads wikipedia:volatile variable . 特别是在C语言中,简单地声明变量volatile不足以从多个线程wikipedia:volatile变量中安全地使用它。

Sharing mutable objects between different threads can be a big headache and cause bugs that are difficult to track down. 在不同线程之间共享可变对象可能是一个令人头疼的问题并导致难以追踪的错误。 Objective-c has a big preference towards immutable objects. Objective-c对不可变对象有很大的偏好。 I'd suggest that if possible, you find a way to pass around immutable objects instead if that is possible. 我建议如果可能的话,你会找到一种方法来传递不可变对象,如果可能的话。 Then you don't have to worry about a shared object being written to at all. 然后,您根本不必担心共享对象被写入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将此Java代码转换为Objective C代码的最佳方法是什么? - What is the best way to convert this java code into Objective C code? 在Objective C中释放内存的最佳方法是什么? - What is the best way to dealloc memory in Objective C? 在目标c中执行选择器主线程的最佳方法是什么? - Best way to performselectoronmainthread in objective c? 从 Objective-C 移植到 C++ 的最佳方法是什么? - What is the best way to port from Objective-C to C++? 在条件语句之前初始化目标c中的变量的最佳方法是什么? - What is the best way to initialise a variable in objective c, before a conditional statement? 将对象传递给Objective-C中另一个类的最佳方法是什么? - What is the best way to pass an object to another class in Objective-C? 硬编码这些数据的最佳方法是什么? Objective-C的 - What is the best way of hardcoding this data. Objective-C 在objective-c(iOS)中转码视频的最佳方法是什么? - What is the best way to transcode a video in objective-c (iOS)? 退出和处理 Objective-C 中的致命错误的最佳方法是什么? - What is the best way to exit and handle a fatal error in Objective-C? 在Objective-C中实现此滚动UI的最佳方法是什么? - What is the best way to implement this scrolling UI in Objective-C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM