grand-central-dispatch

Does pthreads provide any advantages over GCD?

血红的双手。 提交于 2019-12-04 08:58:58
问题 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

Timer inside global queue is not calling in iOS

守給你的承諾、 提交于 2019-12-04 08:41:23
-(void)viewDidLoad{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:@selector(action_Timer) userInfo:nil repeats:YES]; } ); } -(void)action_Timer{ LOG("Timer called"); } action_Timer is not being called. I dont know why. Do you have any idea? You're calling +[NSTimer scheduledTimerWithTimeInterval:...] from a GCD worker thread. GCD worker threads don't run a run loop. That's why your first try didn't work. When you tried [[NSRunLoop mainRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode] ,

Asynchronous UITableViewCell Image Loading Using GCD

混江龙づ霸主 提交于 2019-12-04 08:38:37
问题 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

How to parallelize Sudoku solver using Grand Central Dispatch?

假装没事ソ 提交于 2019-12-04 07:55:51
As a programming exercise, I just finished writing a Sudoku solver that uses the backtracking algorithm (see Wikipedia for a simple example written in C). To take this a step further, I would like to use Snow Leopard's GCD to parallelize this so that it runs on all of my machine's cores. Can someone give me pointers on how I should go about doing this and what code changes I should make? Thanks! Matt For one, since backtracking is a depth-first search it is not directly parallelizable, since any newly computed result cannot be used be directly used by another thread. Instead, you must divide

How to sync serial queue for URLSession tasks?

泄露秘密 提交于 2019-12-04 07:04:50
Using XCode-8.2.1, Swift-3.0.2 and iOS-10.2.1, I am trying to call two different URLSession.shared.dataTasks (the first is a simple URL-request and the second is a POST-request). Since my first dataTask delivers a result that is needed in the httpBody of the second dataTask, the two URLSession.shared.dataTasks shall run in series, one after the other! (and also the preparative code shall run consecutively). I tried, so far, using two consecutive serialQueue.sync{} queues. But I had to realize that the code does not perform in the order I would like to. The print-statement in the log turn out

Nested Dispatch Groups Swift

淺唱寂寞╮ 提交于 2019-12-04 06:44:53
When a user creates a new group in my app I have to push invites to the database as well as other information. I've started using Dispatch Groups in order to keep track of when all the information is successfully sent out so I can dismiss the view. I'm trying to use a dispatch group for the invites and another dispatch group for all the data. Here's what I have: // Push new data to db func createGroup(onSccess completion:@escaping () -> Void) { let inviteDispatchGroup = DispatchGroup() let dataDispatchGroup = DispatchGroup() let uid = FIRAuth.auth()?.currentUser?.uid let name = String(uid!) +

DispatchWorkItem not notifying main thread

浪尽此生 提交于 2019-12-04 06:25:45
问题 Note: This is not duplicate question I have already seen Dispatch group - cannot notify to main thread There is nothing answered about DispatchWorkItem I have code like below let dwi3 = DispatchWorkItem { print("start DispatchWorkItem \(Thread.isMainThread)") sleep(2) print("end DispatchWorkItem") } let myDq = DispatchQueue(label: "A custom dispatch queue") dwi3.notify(queue: myDq) { print("notify") } DispatchQueue.global().async(execute: dwi3) Which is working correctly (I can see notify on

How to lock an NSLock on a specific thread

我的未来我决定 提交于 2019-12-04 06:00:35
I have a property @property NSLock *myLock And I want to write two methods: - (void) lock and - (void) unlock These methods lock and unlock myLock respectively and they need to do this regardless of what thread or queue called them. For instance, thread A might have called lock but queue B might be the one calling unlock . Both of these methods should work appropriately without reporting that I am trying to unlock a lock from a different thread/queue that locked it. Additionally, they need to do this synchronously. It is rare anymore that NSLock is the right tool for the job. There much better

When to dispatch_release()?

怎甘沉沦 提交于 2019-12-04 05:55:35
I'm fairly new to GCD and was trying to find an answer to this. Assuming I have the following code: dispatch_queue_t queue = dispatch_queue_create("queue", NULL); dispatch_async(queue, ^{ // do some stuff }); Where in the code should I release the queue? Inside or outside the block? Outside the block. I'm fairly certain you don't have to wait for the async block to finish as GCD will retain the queue. 来源: https://stackoverflow.com/questions/4842460/when-to-dispatch-release

Waiting for multipart image sending get completed

僤鯓⒐⒋嵵緔 提交于 2019-12-04 05:22:25
问题 I'm impementing an application in iOS7, it's kind of a social network app with posts with images and a backend that saves all of the data sent form the client. The iOS client is sending the information of the post via json and after the info is sent, it starts to send the image via multipart form using AFNetworking . I need to be notified when the image is sent, so that I can refresh the main view of the app with the new posts, including the recently posted by the client. In the practice if I