title: Bad acess, multi-threading, GCD, swift
body: <p>I am trying to translate some sample code from objective-c into swift!</p>
  
  <p>I got it all working except for the multithreading part which is cruical to this simulation.</p>
  
  <p>For some reason when I start using multiple threads it has access errors. Specefically when getting or setting things from the array.</p>
  
  <p>This class is instanced inside of a static class.</p>
  
  <pre><code>var screenWidthi:Int = 0
  var screenHeighti:Int = 0
  var poolWidthi:Int = 0
  var poolHeighti:Int = 0
  
  var rippleSource:[GLfloat] = []
  var rippleDest:[GLfloat] = []
  
  func update()
  {
          let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
  
          dispatch_apply(Int(poolHeighti), queue, {(y: size_t) -&gt; Void in
          //for y in 0..&lt;poolHeighti
          //{
              let pw = self.poolWidthi
              for x in 1..&lt;(pw - 1)
              {
                  let ai:Int = (y    ) * (pw + 2) + x + 1
                  let bi:Int = (y + 2) * (pw + 2) + x + 1
                  let ci:Int = (y + 1) * (pw + 2) + x
                  let di:Int = (y + 1) * (pw + 2) + x + 2
                  let me:Int = (y + 1) * (pw + 2) + x + 1
  
                  let a = self.rippleSource[ai]
                  let b = self.rippleSource[bi]
                  let c = self.rippleSource[ci]
                  let d = self.rippleSource[di]
  
                  var result = (a + b + c + d) / 2.0 - self.rippleDest[me]
                  result -= result / 32.0
  
                  self.rippleDest[me] = result
              }
          }
          )
  }
  </code></pre>
  
  <p>It is important to note that there is also another loop that should run on a different thread right after this one, it acesses the same arrays. That being said it will still bad acess without having the 2nd in another thread so I feel that it is irrelivant to show.</p>
  
  <p>If you could please tell me what is going on that causes this crash to happen at randomish times rather then the first time.</p>
  
  <p>If you want reference here is what it was like in objective c</p>
  
  <pre><code>dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
      dispatch_apply(poolHeight, queue, ^(size_t y) {
          for (int x=0; x&lt;poolWidth; x++)
          {
              float a = rippleSource[(y)*(poolWidth+2) + x+1];
              float b = rippleSource[(y+2)*(poolWidth+2) + x+1];
              float c = rippleSource[(y+1)*(poolWidth+2) + x];
              float d = rippleSource[(y+1)*(poolWidth+2) + x+2];
  
              float result = (a + b + c + d)/2.f - rippleDest[(y+1)*(poolWidth+2) + x+1];
  
              result -= result/32.f;
  
              rippleDest[(y+1)*(poolWidth+2) + x+1] = result;
          }            
      });
  </code></pre>
  
  <p>How do you ensure that variables are able to be accessed from different threads? How about static members?</p>
  
  <p>I only no how to print out the call stack before the app crashes, however after, the only way I know to get to the call stack is to look at the threads. Let me know if there is a different way I should do this.
  <a href="https://i.stack.imgur.com/elRlB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/elRlB.png" alt="enter image description here"></a></p>
  
  <p>NOTE: I noticed something wierd. I put a print statement in each loop so I could see what x and y coordinate it was processing to see if the crash was consistant. Obiously that brought the fps down to well under 1 fps, however I did notice it has yet to crash. The program is running perfect so far without any bad acess just at under 1 fps.</p>