title: Swift DispatchQueue concurrentPerform OpenGL parallel rendering
body: <p>I have a headless EGL renderer in c++ for Linux that I have wrapped with bindings to use in Swift. It works great – I can render in parallel creating multiple contexts and rendering in separate threads, but I've run into a weird issue. First of all I have wrapped all GL calls specific to a renderer and it's context inside it's own serial queue like below.</p>
  
  <pre><code>func draw(data:Any) -&gt; results {
    serial.sync {
      //All rendering code for this renderer is wrapped in a unique serial queue.
      bindGLContext()
      draw()
    }
  }
  </code></pre>
  
  <p>To batch data between renderers I used DispatchQueue.concurrentPerform. It works correctly, but when I try creating a concurrent queue with a DispatchGroup something weird happens. Even though I have wrapped all GL calls in serial queues the GL contexts get messed up and all gl calls fail to allocate textures/buffers/etc.</p>
  
  <p>So I am trying to understand the difference between these two and why one works and the other doesn't. Any ideas would be great!</p>
  
  <pre><code>//This works
  DispatchQueue.concurrentPerform(iterations: renderers.count) { j in
    let batch = batches[j]
    let renderer = renderers[j]
    let _ = renderer.draw(data:batch)
  }
  </code></pre>
  
  <pre><code>//This fails – specifically GL calls fail
  let group = DispatchGroup()
  let q = DispatchQueue(label: "queue.concurrent", attributes: .concurrent)
  for (j, renderer) in renderers.enumerated() {
     q.async(group: group) {
       let batch = batches[j]
       let _ = renderer.draw(data:batch)
     }
  }
  group.wait()
  </code></pre>