0

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?

bobby123uk
  • 892
  • 4
  • 17
  • You can’t interrupt a synchronous function in a single thread environment – Joakim Danielson Mar 17 '22 at 08:21
  • The linked post says 'The key for avoiding confusion is that reentrant refers to only one thread executing. It is a concept from the time when no multitasking operating systems existed.' So how can re-entrant be defined as interupting a functions execution? – bobby123uk Mar 17 '22 at 13:24
  • The article mentions recursion and callbacks. When a recursively called function is returning the calling function is re-entered. So that is a form of interruption with reentrance – Joakim Danielson Mar 17 '22 at 13:58
  • Would you like to post this as an official answer which can be voted upon @JoakimDanielson ? – bobby123uk Mar 18 '22 at 16:08
  • No I don't see it as an answer, more a clarification of what was written in the answer you linked to. – Joakim Danielson Mar 18 '22 at 16:22

1 Answers1

0

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.

bobby123uk
  • 892
  • 4
  • 17