CircularBuffer

Thread-Safe Circular Buffer Implementation

Requirements

  • Xcode 8.2
  • Swift 3.0

Q&A

Q1: Can you remove semaphores (or whatever thread-safe technique you employed) from the implementation, but guarantee that the implementation remains thread safe?

No, making the the indicies atomic will not make it thread-safe.

Q2a: If you can remove semaphores, show how you'd do it, explain why it is thread safe, and compare the speed of the two implementations (with and without semaphores) Lets say you put 5 elements in; get one element; put another; get 1; put another, etc. for a sufficiently large number of iterations. Assume the array size is 10.

I removed the queue used to make it thread-safe, you can check the code here (It's in a different branch). At first, it looked like it was working correctly, using just 2 threads. However, after increasing the number of threads the application started to fail randomly. Using atomic increament should be faster than having my own queue, because the command it's mapped to a CPU atomic instructions

Q2b: If you cannot, explain why the implementation would not be thread safe.

Because, I'm writing on a mutable array from different threads, where each thread is trying to assign a different value at the same index

Q3: If the answer to Q1 is yes, can you write a test that will fail if the implementation is not thread-safe?

I was not able to write a test that guarantes the implepentation was thread-safe, what I did was create a test that checks the buffer consistency when you try to write two elements at the same time from different threads

Q4a: Will the buffer be thread safe if there are multiple reader threads? Why?

Yes, because the application is reading from the mutable array, not changing any state

Q4b: Will the buffer be thread safe if there are multiple writer threads? Why?

No, assining values to a mutable array from different threads will crash the application

Q5: if the index increments are not atomic, can the ring buffer still be thread safe?

By default, no, you will need so mechanism to assure thread safety