grand-central-dispatch

Does pthreads provide any advantages over GCD?

牧云@^-^@ 提交于 2019-12-03 01:37:52
Having recently learned Grand Central Dispatch, I've found multithreaded code to be pretty intuitive(with GCD). I like the fact that no locks are required(and the fact that it uses lockless data structures internally), and that the API is very simple. Now, I'm beginning to learn pthreads, and I can't help but be a little overwhelmed with the complexity. Thread joins, mutexes, condition variables- all of these things aren't necessary in GCD, but have a lot of API calls in pthreads. Does pthreads provide any advantages over GCD? Is it more efficient? Are there normal-use cases where pthreads can

GCD Poor Performance

旧城冷巷雨未停 提交于 2019-12-03 01:32:42
问题 As you may remember, I am trying to use GCD to speed up some of my code, namely a collision detection and resolution engine. However, I am clearly doing something wrong because all of my GCD code is significantly slower and less consistent than my serial code (between 1.4x and 10x slower). Allow me to give you an example: I am iterating over an array in a bubble-sort fashion to determine all possible collisions among objects in that array: - (double) detectCollisionsInArray:(NSArray*)objects

dispatch_sync vs. dispatch_async on main queue

前提是你 提交于 2019-12-03 00:29:29
问题 Bear with me, this is going to take some explaining. I have a function that looks like the one below. Context: "aProject" is a Core Data entity named LPProject with an array named 'memberFiles' that contains instances of another Core Data entity called LPFile. Each LPFile represents a file on disk and what we want to do is open each of those files and parse its text, looking for @import statements that point to OTHER files. If we find @import statements, we want to locate the file they point

FIFO serial queue using GCD

自作多情 提交于 2019-12-02 23:35:40
I am trying to create a (network) synchronized array for the company I work for. While the networking part works fine, I have dwelled into an issue. My wish was to create a new queue using dispatch_create_queue , to which I would add two blocks that are not to run on the main thread, but in a serial manner, meaning that first the first block has to run, then the second, and never in parallel. I've read the apple documentation, but it is confusing to say the least. When I create my queue using dispatch_queue_create and then add the blocks (after they have been defined) using dispatch_sync , I

Good pattern for Internet requests with Grand Central Dispatch?

妖精的绣舞 提交于 2019-12-02 23:29:19
I'm currently using synchronous ASIHTTPRequest with GCD queues to download data from the Internet, then parse the response data with JSONKit. What do you think about this pattern. Thank you in advance. Here is my code: dispatch_async(queue, ^(void) { // Request is ASIHTTPRequest. [request startSynchronous]; // Parse JSON. NSArray *array = [[request responseData] objectFromJSONDataWithParseOptions:JKParseOptionLooseUnicode]; // Callback on the main queue to update UI. dispatch_async(dispatch_get_main_queue(), ^(void) { callbackBlock(array); }); }); EDIT: The reason I use ASIHTTPRequest is that

Asynchronous UITableViewCell Image Loading Using GCD

江枫思渺然 提交于 2019-12-02 23:01:44
I'm currently trying to load a UITableView list of Flickr Photo (cs193p iOS Stanford, assignment 5). To avoid UI blocking event, I've deferred the thumbnail download of each cell into a different queue (but do update the UI back in the main queue). This code doesn't asynchronously load the images, though does add a thumbnail once I click on of the UITableViewCell row. (see screenshots below). Any idea what i'm doing wrong? PS: I've looked already in a few other stackoverflow questions & Apple's LazyTableImages example, but I remain convinced this is the cleanest way to achieve the desired

OperationQueue.main vs DispatchQueue.main

主宰稳场 提交于 2019-12-02 22:12:06
When you need to perform something on the main thread in the completion block of a networking task or an operation, which of these ways to get it would be the most appropriate and why?: OperationQueue.main.addOperation DispatchQueue.main.async MrMage For details on the differences between the two types of queue, see Lion's answer. Both approaches will work. However, NSOperation is mostly needed when more advanced scheduling (including dependencies , canceling , etc.) is required. So in this case, a simple DispatchQueue.main.async { /* do work */ } will be just fine. That would be equivalent to

Easy example of Grand Central Dispatch

放肆的年华 提交于 2019-12-02 20:49:14
I'm newbie programming for mac and i'm really surprised on Grand Central Dispatch. I read about that and looks like the perfect solution for parallel programming. I worked with POSIX threads and want to move to GCD. I saw the samples codes in the Apple Developer Connection, but It confused me so much. I searched for an easy example with two threads to start but i can't find it. How can I do this sample code using GCD ??? #include <stdio.h> /* standard I/O routines */ #include <pthread.h> /* pthread functions and data structures */ /* function to be executed by the new thread */ void* do_loop

Apple doc's GCD Producer-Consumer solution wrong?

核能气质少年 提交于 2019-12-02 20:45:51
In the Migrating Away from Threads section of Apple's Concurrency Programming Guide, there is Changing Producer-Consumer Implementations , which claims that the typical multistep pthread mutex + condition variable implementation can be simplified using GCD. With dispatch queues, you can simplify the producer and consumer implementations into a single call: dispatch_async(queue, ^{ // Process a work item. }); When your producer has work to be done, all it has to do is add that work to a queue and let the queue process the item. The Producer-Consumer problem is also known as the Bounded-Buffer

Could Grand Central Dispatch (`libdispatch`) ever be made available on Windows?

我的梦境 提交于 2019-12-02 20:42:57
I’m looking into multithreading, and GCD seems like a much better option than manually writing a solution using pthread.h and pthreads-win32 . However, although it looks like libdispatch is either working on, or soon going to be working on, most newer POSIX-compatible systems… I have to ask, what about Windows? What are the chances of libdispatch being ported to Windows? What are the barriers preventing that from happening? If it came down to it, what would I need to do to preform that portage? Edit: Some things I already know, to get the discussion started: We need a blocks-compatible