4 ms·
I see how GCD is going to make putting long running tasks on a separate thread easy, but I have a bone to pick. In his GCD example he has this example of code
by mronge 17y ago
I see how GCD is going to make putting long running tasks on a separate thread easy, but I have a bone to pick. In his GCD example he has this example of code
- (IBAction)analyzeDocument:(NSButton *)sender
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSDictionary *stats = [myDoc analyze];
dispatch_async(dispatch_get_main_queue(), ^{
[myModel setDict:stats];
[myStatsView setNeedsDisplay:YES];
[stats release];
});
});
}
It takes a potentially long running operation, analyze, and put it on a background thread. Once it's done, the UI is updated on the main thread. GCD made that ridiculously easy, but, this is assuming that the objects reference from your block are thread-safe.
It may be that the analyze method isn't thread safe, and mutates some state inside the myDoc object. So it made one part of creating async/multi-threaded apps easier, but you still need to be careful with mutating state like that.
- antonovka 17y agoIt may be that the analyze method isn't thread safe, and mutates some state inside the myDoc object. So it made one part of creating async/multi-threaded apps easier, but you still need to be careful with mutating state like that. Yes. I believe many Mac developers attempting to leverage GCD are going to get a long-overdue wakeup call regarding their extremely prevalent use of mutable state.