ios - Mutex alternatives in swift -


i have shared-memory between multiple threads. want prevent these threads access piece of memory @ same time. (like producer-consumer problem)

problem:

a thread add elements queue , thread reads these elements , delete them. shouldn't access queue simultaneously.

one solution problem use mutex.

as found, there no mutex in swift. there alternatives in swift?

as people commented (incl. me), there several ways achieve kind of lock. think dispatch semaphore better others because seems have least overhead. found in apples doc, "replacing semaphore code", doesn't go down kernel space unless semaphore locked (= zero), case when code goes down kernel switch thread. think semaphore not 0 of time (which of course app specific matter, though). thus, can avoid lots of overhead.

one more comment on dispatch semaphore, opposite scenario above. if threads have different execution priorities, , higher priority threads have lock semaphore long time, dispatch semaphore may not solution. because there's no "queue" among waiting threads. happens @ case higher priority threads , lock semaphore of time, , lower priority threads can lock semaphore occasionally, thus, waiting. if behavior not application, have consider dispatch queue instead.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -