Interrupting a function's execution

172 views Asked by At

I found a great post explaining the re-entrancy problem but I was curious to know the various ways in which a function can be interrupted in a single-threaded environment.

final class Radiator {
    
    private(set) var modelName: String
    private(set) var serialNumber: Double
    
    init(modelName: String, serialNumber: Double) {
        self.modelName = modelName
        self.serialNumber = serialNumber
    }
    
    func update(modelName: String, serialNumber: Double) {
        self.modelName = modelName
        self.serialNumber = serialNumber
    }
}

If we created an instance of the above in memory with some initial values and then we called the update function a little later on, how would the above update function be interrupted mid execution in such a way so that the model name received a new value but the serial number did not?

Another example might be a function that opens a file, manipulates a file and then closes the file. An interruption might fail to close the file. How would that come about in a single threaded environment?

1

There are 1 answers

0
bobby123uk On

I appreciate the clarification offered by @joakim Danielson in the comments.

An interruption in this context doesn't necessarily mean the execution of some other invocation is stopped. Rather, an additional invocation has started before the previous has been completed.