1 Reply
      Latest reply on Jan 26, 2019 1:35 PM by Ken Thomases
      Eonil Level 1 Level 1 (0 points)

        macOS lacks unnamed semaphore implementations. As named semaphores are not private, they're not realiable as they can be controlled publicly via name. I see DispatchSemaphore is implemented using `sem_init` which is unnamed semaphores. It seems unnamed semaphores are allowed only for system code. If DispatchSemaphore is simply a wrapper around pthread unnamed semaphore, it would be safe to use them to control pthread. But as it is a GCD level object, I actually don't know whether they're actually safe for pthread.


        Is it safe to use DispatchSemaphore with pthread stuffs?

        • Re: Is it safe to use DispatchSemaphore with pthread?
          Ken Thomases Level 4 Level 4 (765 points)

          Yes, it's safe.  Pthreads is the foundation for threading in higher-level APIs like GCD, so GCD worker threads are also pthreads (although you don't own them, so you must not manipulate them via the pthreads API).


          Also, a typical use case for dispatch semaphore is to coordinate between a non-GCD thread and a GCD task.  For example, some code dispatches a task asynchronously and then, when it needs the result, waits on a semaphore that the task is to signal when it's done.  There's no requirement that the code doing that be on a GCD thread.