grand-central-dispatch

How can I run code block in background periodically using GCD?

删除回忆录丶 提交于 2019-12-03 16:37:01
问题 How can I run code block in background periodically using GCD? I am trying to write a game engine with several subsystems, like rendering, physics, game logic and so on. Some tasks should be event-driven, but some (like physics system) should be called periodically in the background with constant time (for example after 1/100 sec). I created a block of code, but how can I run this block periodically in background? Is GCD right tool here? 回答1: What you want is a GCD dispatch source. For sample

How to implement a reentrant locking mechanism through dispatch concurrent queue (GCD)?

不打扰是莪最后的温柔 提交于 2019-12-03 15:44:54
I have just read this post , and its solution seems convincing: Serial queue is used to synchronize access dispatch_get_specific/dispatch_set_specific is used to provide reentrance capability. What I am interested at is if it is possible to advance this scheme to implement reentrant locking mechanism for concurrent dispatch queue (each read is done using dispatch_sync, write is done using dispatch_barrier_async, like is described here, see "One Resource, Multiple Readers, and a Single Writer" ). P.S. I think I've managed to implement this using [NSThread currentThread].threadDictionary here ,

Wait for completion handler to finish - Swift

馋奶兔 提交于 2019-12-03 15:25:15
I am trying to check if UserNotifications are enabled and if not I want to throw an alert. So I have a function checkAvailability which checks multiple things, including the UserNotification authorization status. func checkAvailabilty() -> Bool { // // other checking // var isNotificationsEnabled = false UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound], completionHandler: { (granted, error) in if granted { isNotificationsEnabled = true } else { isNotificationsEnabled = false } }) } if isNotificationsEnabled { return true } else { // Throw alert: Remind user to

Risk Assessment: Using Pthreads (vs. GCD or NSThread)

雨燕双飞 提交于 2019-12-03 13:33:54
A colleague suggested recently that I use pthreads instead of GCD because it's, "way faster." I don't disagree that it's faster, but what's the risk with pthreads? My feeling is that they will ultimately not be anywhere nearly as idiot-proof as GCD (and my team of one is 50% idiots). Are pthreads hard to get right? GCD and pthreads are both ways of doing work asynchronously, but they are significantly different. Most descriptions of GCD describe it in terms of threads and of thread pooling, but as DrPizza puts it to concentrate on [threads and thread pools] is to miss the point. GCD’s value

Multi-threaded Objective-C accessors: GCD vs locks

梦想的初衷 提交于 2019-12-03 12:42:39
I'm debating whether or not to move to a GCD-based pattern for multi-threaded accessors. I've been using custom lock-based synchronization in accessors for years, but I've found some info ( Intro to GCD ) and there seems to be pros of a GCD-based approach. I'm hoping to start a dialog here to help myself and others weigh the decision. The pattern looks like: - (id)something { __block id localSomething; dispatch_sync(queue, ^{ localSomething = [something retain]; }); return [localSomething autorelease]; } - (void)setSomething:(id)newSomething { dispatch_async(queue, ^{ if(newSomething !=

GCD, NSOperationQueue, or create a thread manually?

余生颓废 提交于 2019-12-03 12:38:33
问题 When you use threads, do you have any preferences? In general rule, to use any of these techniques : create a new thread manually and use the run loop use NSOperationQueue or use Grand Central Dispatch and the C version with dispatch_queue? Does NSOperationQueue simplify everything, and thus is better to be used when we need to create an asynchronous function? 回答1: I'm lazy, so my philosophy is to pick the simplest solution that does everything I need it to. (I like to think this is the "lazy

NSNotification vs. dispatch_get_main_queue

夙愿已清 提交于 2019-12-03 12:25:27
问题 In relation to this question I was wondering if there is any generally accepted logic regarding when to use NSNotification, with an observer in your main thread, vs using GCD to dispatch work from a background thread to the main thread? It seems that with a notification-observer setup you have to remember to tear down the observer when your view unloads but then you reliably ignore the notification, where as dispatching a job to the main thread may result in a block being executed when the

GCD: How to remove waiting tasks from serial queue?

故事扮演 提交于 2019-12-03 12:21:51
First I create a serial queue like this static dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL); then, at some unknown point in time a task gets added to the queue like this dispatch_async(queue, ^{ // do something, which takes some time }); If the first task hasn't finished yet, the new task will wait until the first completes (that's of course what a serial queue is for). But if I add 5 new tasks to the queue, while the original first one is still running, I don't want to execute new task no.1, then no.2, then no.3 and so on, but want to get rid of tasks 1 to

Is NSNotificationCenter thread safe?

五迷三道 提交于 2019-12-03 12:00:48
Can I post a notification in a given queue and receive it on another? I want to use notifications to communicate different queues, but I'm not sure if this is safe... No. Notifications are delivered in the same thread that they are sent from, this you will need to re-send it in some way to get the notification to your thread. No. Apple's docs on the subject say: " Regular notification centers deliver notifications on the thread in which the notification was posted. [...] At times, you may require notifications to be delivered on a particular thread that is determined by you instead of the

Referencing an NSOperation object in its own completion block with ARC

独自空忆成欢 提交于 2019-12-03 12:00:47
问题 I'm having difficulty converting some NSOperation code to ARC. My operation object uses a completion block, which in turn contains a GCD block that updates the UI on the main thread. Because I reference my operation object from inside its own completion block, I'm using a __weak pointer to avoid a memory leak. However, the pointer is already set to nil by the time my code runs. I've narrowed it down to this code sample. Anyone know where I went wrong, and the right way to accomplish this?