grand-central-dispatch

Why does this code cause “EXC_BAD_INSTRUCTION”?

时光怂恿深爱的人放手 提交于 2019-12-18 10:52:49
问题 dispatch_semaphore_t aSemaphore = dispatch_semaphore_create(1); dispatch_semaphore_wait(aSemaphore, DISPATCH_TIME_FOREVER); dispatch_release(aSemaphore); When the program runs to dispatch_release(aSemaphore) , it will cause "EXC_BAD_INSTRUCTION", and then crash. Why? 回答1: I tried this code and it does indeed die with illegal instruction. So I did some digging and found that it's dying in _dispatch_semaphore_dispose. So let's look at what that is (ARMv7 here, because it's easy to understand!):

How many blocks are running on a queue?

余生长醉 提交于 2019-12-18 09:25:50
问题 Any way to check how many blocks are running on a concurrent queue? I want to limit running blocks in a concurrent queue, and add a new block only if this number is lower i.e. 3. Any way to do it? The reason I read here, that maximum number of threads is 64. Above it app will freeze. 来源: https://stackoverflow.com/questions/29731285/how-many-blocks-are-running-on-a-queue

iOS Concurrency - Not reaching anywhere's near theoretical maximum

一世执手 提交于 2019-12-18 09:24:11
问题 I'm new to Grand Central Dispatch and have been running some tests with it doing some processing on an image. Basically I'm running a grayscale algorithm both sequentially and using GCD and comparing the results. here is the basic loop: UInt8 r,g,b; uint pixelIndex; for (uint y = 0; y < height; y++) { for (uint x = 0; x < width; x++) { pixelIndex = (uint)(y * width + x); if (pixelIndex+2 < width * height) { sourceDataPtr = &sourceData[pixelIndex]; r = sourceDataPtr[0+0]; g = sourceDataPtr[0+1

iOS MKMapShapshotter completion block is not always being called

一个人想着一个人 提交于 2019-12-18 07:46:19
问题 I am trying to use the new iOS7 MKMapSnapshotter to generate a static map image. Whenever my app needs a map, I call the following: MKMapSnapshotter *snapshotter = [[[MKMapSnapshotter alloc] initWithOptions:theOptions] autorelease]; dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); DebugLog(@"Snapshotter allocated %@ and run on queue %@", snapshotter, aQueue); [snapshotter startWithQueue:aQueue completionHandler:^(MKMapSnapshot *snapshot, NSError

Can I limit concurrent requests using GCD?

≡放荡痞女 提交于 2019-12-18 04:54:32
问题 I'm acquiring data from a database asynchronously. Is there any way I can limit the concurrent requests to a number, but still execute the rest? I saw a post using NSOperation and NSOperationQueue but this is too complicated for me. Is there any other ways to do it? Can GCD achieve it? 回答1: Something like this: ... //only make one of these obviously :) remaking it each time you dispatch_async wouldn't limit anything dispatch_semaphore_t concurrencyLimitingSemaphore = dispatch_semaphore_create

Intangible Order of Execution (dispatch_semaphore_t, dispatch_group_async) and the Use of Them in Combination with Different Dispatch Queue Types

烂漫一生 提交于 2019-12-18 02:54:20
问题 I just took some time in the evening to play around with GCD, especially with dispatch_semaphore_t because I never used it. Never had the need to. So I wrote the following as a test: - (void)viewDidLoad { UIView *firstView = [[UIView alloc] initWithFrame:(CGRect){{0, 0}, self.view.frame.size.width/4, self.view.frame.size.width/5}]; firstView.backgroundColor = [UIColor purpleColor]; [self.view addSubview:firstView]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),

Order of operations in runloop on iOS

跟風遠走 提交于 2019-12-17 22:37:26
问题 What is the order of operations on iOS? I'm thinking sepcifically about timing of setNeedsLayout and layoutSubviews setNeedsDisplay and drawRect touch recognition [NSTimer scheduledTimerWithTimeInterval:0.000001 tar(...)] dispatch_async(dispatch_get_main_queue(), ^{ /* code */} As an example of an answer I would like to receive it could be in this format: dispatch_async on main Happens before the next runcycle drawRect Happens at the end of the runcycle 回答1: (Parts of this are copied from my

Grand Central Dispatch async vs sync [duplicate]

强颜欢笑 提交于 2019-12-17 22:09:58
问题 This question already has answers here : Difference between DispatchQueue.main.async and DispatchQueue.main.sync (3 answers) Closed 2 months ago . I'm reading the docs on dispatch queues for GCD, and in it they say that the queues are FIFO, so I am woundering what effect this has on async / sync dispatches? from my understand async executes things in the order that it gets things while sync executes things serial.. but when you write your GCD code you decide the order in which things happen..

`[NSThread isMainThread]` always returns YES

别来无恙 提交于 2019-12-17 20:18:30
问题 This code dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ NSLog(@"Main Thread? %d", [NSThread isMainThread]); }); shows that I'm in the main thread. Even doing this: queue = dispatch_queue_create("nonMainQueue", NULL); still reports that I'm in the main queue. This is, it seems, because I'm using dispatch sync. Does this mean that my code is the same as not using dispatch_sync at all? Also: what's the point of dispatch_sync if it does nothing at all, then? 回答1:

How do I execute code once and only once in Swift?

拈花ヽ惹草 提交于 2019-12-17 19:27:16
问题 The answers I've seen so far (1, 2, 3) recommend using GCD's dispatch_once thus: var token: dispatch_once_t = 0 func test() { dispatch_once(&token) { print("This is printed only on the first call to test()") } print("This is printed for each call to test()") } test() Output: This is printed only on the first call to test() This is printed for each call to test() But wait a minute. token is a variable, so I could easily do this: var token: dispatch_once_t = 0 func test() { dispatch_once(&token