Search code examples
objective-ciosmultithreadinglocks

Do I need to use NSLock?


I have an iOS class which does some calculations in a separate thread while the main thread shows an alert that allows the user to cancel the calculation. The calculation thread uses several instance variables which are accessible from both the main thread and the calculation thread. The calculation thread only ever reads these variables, and the main thread does not write or read these variables while the calculation thread exists.

My question is, to I need to lock these variables?


Solution

  • As long as only one thread accesses them, no, you don't need to lock them. It might be good practice to try though, and doing it now would be beneficial in case you ever do want to access them from the main thread.