grand-central-dispatch

Is this safe to call wait() of DispatchSemaphore several times at one time?

淺唱寂寞╮ 提交于 2019-12-05 06:28:45
I got three dispatched threads named queueA, queueB, queueC. Now I want the queueA executed after queueB and queueC done. So I tried to implement it by DispatchSemaphore . My Problem is: Is this safe to call wait() two times in a thread at one time to make the semaphore 2? self.semaphore.wait() // +1 self.semaphore.wait() // +1 The following is the entire test code: class GCDLockTest { let semaphore = DispatchSemaphore(value: 0) func test() { let queueA = DispatchQueue(label: "Q1") let queueB = DispatchQueue(label: "Q2") let queueC = DispatchQueue(label: "Q3") queueA.async { self.semaphore

Why is it necessary to call dispatch_group_leave the same number of times as dispatch_group_enter?

China☆狼群 提交于 2019-12-05 05:41:31
In my dispatch_group code, I use dispatch_group_wait to time out a group of web service calls. Question, it seems like I would need to count how many times dispatch_group_enter is called and then call the same number of remaining dispatch_group_leave should some web service calls never return causing an unequal number of dispatch_group_enter vs dispatch_group_leave. Why? I have seen crashes happening if I don't do this in the case when timeout happens and triggers dispatch_group_wait. The crash goes away as soon as make sure dispatch_group_enter/dispatch_group_leave match their call count.

NSOperationQueue, concurrent operation and thread

白昼怎懂夜的黑 提交于 2019-12-05 05:21:11
问题 I'm developing a sort of fast image scan application. In the -captureOutput:didOutputSampleBuffer:fromConnection: method I pick up the CVPixelBuffer and add them into an NSOperation subclass. The NSOperation takes the buffer and transform it into an image saved on the filesystem. The -isConcurrent method of NSOperation returns YES. After the operation is created, is added to an NSOperationQueue . Everything runs fine except for one issue that is affecting the scan frame rate. Using time

dispatch_semaphore_t reuse - What am I missing here?

余生颓废 提交于 2019-12-05 03:46:54
I have some code where I am using dispatch_semaphore_t to signal operation completion. When the semaphore is a member variable, it does not seem to behave correctly. I will show example code that works and an example that does not seem to work: @implementation someClass { dispatch_semaphore_t memberSem; dispatch_semaphore_t* semPtr; NSThread* worker; BOOL taskDone; } - (id)init { // Set up the worker thread and launch it - not shown here. memberSem= dispatch_semaphore_create(0); semPtr= NULL; taskDone= FALSE; } - (void)dealloc { // Clean up the worker thread as needed - not shown here. if(

is this GCD implemented getter setter thread safe and work better than @synchronized? objc

混江龙づ霸主 提交于 2019-12-05 02:54:22
问题 @interface ViewController () @property (nonatomic, strong) NSString *someString; @end @implementation ViewController @synthesize someString = _someString; - (NSString *)someString { __block NSString *tmp; dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ tmp = _someString; }); return tmp; } - (void)setSomeString:(NSString *)someString { __block NSString *tmp; dispatch_barrier_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ tmp = someString;

Use dispatch_async to analyze an array concurrently in Swift

谁说胖子不能爱 提交于 2019-12-05 02:01:33
问题 I am trying to analyze a photo concurrently using a background thread from GCD. Here is the code I have written: dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_UTILITY.value), 0)) { for (var i = 0; i < 8; i++) { let color = self.photoAnalyzer.analyzeColors(imageStrips[i]) colorList.append(color) } } For clarification on the variable names, here are their descriptions: photoAnalyzer is an instance of a class I wrote called Analyzer that holds all of the methods to process the image.

Should I avoid creating JSContexts in global queues?

大憨熊 提交于 2019-12-05 01:08:35
问题 I've just had a crash log from a customer's device, and it's crashing here: dispatch_async(dispatch_get_global_queue(0, 0), ^{ JSContext *javaScriptContext = [[JSContext alloc] init]; Here's the crash log: Thread 11 Crashed: 0 JavaScriptCore 0x31009cd6 WTFCrash + 54 1 JavaScriptCore 0x30e0edf6 WTF::OSAllocator::reserveAndCommit(unsigned long, WTF::OSAllocator::Usage, bool, bool, bool) + 166 2 JavaScriptCore 0x30e0ed2a WTF::OSAllocator::reserveUncommitted(unsigned long, WTF::OSAllocator::Usage

using dispatch_sync as a mutex lock

烂漫一生 提交于 2019-12-05 00:40:33
问题 Here is what I need to do. I hope dispatch_sync would be the best way to do it using GCD I have a certain piece of critical section code that is placed in the applicationDidBecomeActive callback in Appdelegate.. I am wrapping up that method inside a dispatch_sync call so that it gets called only once no matter how many times applicationDidBecomeActive is called - (void)applicationDidBecomeActive:(UIApplication *)application{ dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY

Wait for completion handler to finish - Swift

你。 提交于 2019-12-05 00:18:59
问题 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 {

Deadlock with dispatch_sync

僤鯓⒐⒋嵵緔 提交于 2019-12-04 23:49:22
问题 { dispatch_queue_t myQueue = dispatch_queue_create("com.mycompany.myqueue", 0); dispatch_sync(myQueue, ^{ //Do EXTREME PROCESSING!!! for (int i = 0; i< 100; i++) { [NSThread sleepForTimeInterval:.05]; NSLog(@"%i", i); } dispatch_sync(dispatch_get_main_queue(), ^{ [self updateLabelWhenBackgroundDone]; }); }); } I am getting a deadlock here. According to Apple documentation "dispatch_sync": "Submits a block to a dispatch queue for synchronous execution. Unlike dispatch_async, this function does