grand-central-dispatch

method not called with dispatch_async and repeating NSTimer

≡放荡痞女 提交于 2019-11-27 18:26:03
问题 I am developing an app where i want to call method in separate queue using dispatch_async . I want to call that method repeatedly after certain interval of time. But the method is not getting called. I don't know whats wrong. Here is my code: dispatch_async( NotificationQueue, ^{ NSLog(@"inside queue"); timer = [NSTimer scheduledTimerWithTimeInterval: 20.0 target: self selector: @selector(gettingNotification) userInfo: nil repeats: YES]; dispatch_async( dispatch_get_main_queue(), ^{ // Add

C++11 Thread safety of Random number generators

自闭症网瘾萝莉.ら 提交于 2019-11-27 18:22:01
In C++11 there are a bunch of new Random number generator engines and distribution functions. Are they thread safe? If you share a single random distribution and engine among multiple threads, is it safe and will you still receive random numbers? The scenario I am looking is something like, void foo() { std::mt19937_64 engine(static_cast<uint64_t> (system_clock::to_time_t(system_clock::now()))); std::uniform_real_distribution<double> zeroToOne(0.0, 1.0); #pragma omp parallel for for (int i = 0; i < 1000; i++) { double a = zeroToOne(engine); } } using OpenMP or void foo() { std::mt19937_64

File monitoring using Grand Central Dispatch

可紊 提交于 2019-11-27 18:12:24
问题 I'm using the code example by David Hamrick to monitor a file using GCD. int fildes = open("/path/to/config.plist", O_RDONLY); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE,fildes, DISPATCH_VNODE_DELETE | DISPATCH_VNODE_WRITE | DISPATCH_VNODE_EXTEND | DISPATCH_VNODE_ATTRIB | DISPATCH_VNODE_LINK | DISPATCH_VNODE_RENAME | DISPATCH_VNODE_REVOKE, queue); dispatch_source_set_event

GCD and async NSURLConnection

≡放荡痞女 提交于 2019-11-27 18:01:43
I know that if I create an NSURLConnection (standard async one), it will call back on the same thread. Currently this is on my main thread. (work fine too). But i'm now using the same code for something else, and I need to keep my UI snappy.... If i do dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ /* and inside here, at some NSURLConnection is created */ }); .. is it possible that my NSURLConnection is created but my thread disappears before the url connection has returned? I'm new to GCD. How would one keep the thread alive until my url connection returned,

What property should I use for a Dispatch Queue after ARC?

女生的网名这么多〃 提交于 2019-11-27 17:59:18
I maintain a dispatch queue as a property with my view controller. I create this queue once in my view controller's init method, and reuse a few times for some background tasks. Before ARC, I was doing this: @property (nonatomic, assign) dispatch_queue_t filterMainQueue; And in init: if (filterMainQueue == nil) { filterMainQueue = dispatch_queue_create("com.myQueue.CJFilterMainQueue", NULL); } But after ARC, I'm not sure if this should still be "assign", or should it be "strong" or "weak". The ARC convertor script didn't change anything but I'm not sure if a subtle bug is coming from the fact

dispatch_sync on main queue hangs in unit test

爱⌒轻易说出口 提交于 2019-11-27 17:49:54
I was having some trouble unit testing some grand central dispatch code with the built in Xcode unit testing framework, SenTestingKit. I managed to boil my problem done to this. I have a unit test that builds a block and tries to execute it on the main thread. However, the block is never actually executed, so the test hangs because it's a synchronous dispatch. - (void)testSample { dispatch_sync(dispatch_get_main_queue(), ^(void) { NSLog(@"on main thread!"); }); STFail(@"FAIL!"); } What is it about the testing environment that causes this to hang? BJ Homer dispatch_sync runs a block on a given

Pattern for unit testing async queue that calls main queue on completion

孤人 提交于 2019-11-27 17:43:08
This is related to my previous question , but different enough that I figured I'd throw it into a new one. I have some code that runs async on a custom queue, then executes a completion block on the main thread when complete. I'd like to write unit test around this method. My method on MyObject looks like this. + (void)doSomethingAsyncThenRunCompletionBlockOnMainQueue:(void (^)())completionBlock { dispatch_queue_t customQueue = dispatch_queue_create("com.myObject.myCustomQueue", 0); dispatch_async(customQueue, ^(void) { dispatch_queue_t currentQueue = dispatch_get_current_queue(); dispatch

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) on dispatch_semaphore_dispose

喜夏-厌秋 提交于 2019-11-27 17:31:14
I am getting EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) on dispatch_semaphore_dispose but don't really know how to track down the root cause of this. My code makes use of dispatch_async, dispatch_group_enter and so on. UPDATE: The cause of the crash is due to the fact that the webserviceCall (see code below) never calls onCompletion and when the code is run again, I got the error EXC_BAD_INSTRUCTION. I verified this is indeed the case, but not sure why or how to prevent this. Code: dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_group_t

NSThread vs. NSOperationQueue vs. ??? on the iPhone

二次信任 提交于 2019-11-27 17:27:38
Currently I'm using NSThread to cache images in another thread. [NSThread detachNewThreadSelector:@selector(cacheImage:) toTarget:self withObject:image]; Alternately: [self performSelectorInBackground:@selector(cacheImage:) withObject:image]; Alternately, I can use an NSOperationQueue NSInvocationOperation *invOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(cacheImage:) object:image]; NSOperationQueue *opQueue = [[NSOperationQueue alloc] init]; [opQueue addOperation:invOperation]; Is there any reason to switch away from NSThread ? GCD is a 4th option when it's

Grand Central Dispatch vs NSThreads?

喜你入骨 提交于 2019-11-27 17:21:05
I searched a variety of sources but don't really understand the difference between using NSThreads and GCD. I'm completely new to the OS X platform so I might be completely misinterpreting this. From what I read online, GCD seems to do the exact same thing as basic threads (POSIX, NSThreads etc.) while adding much more technical jargon ("blocks"). It seems to just overcomplicate the basic thread creation system (create thread, run function). What exactly is GCD and why would it ever be preferred over traditional threading? When should traditional threads be used rather than GCD? And finally is