GCD: How to remove waiting tasks from serial queue?

后端 未结 3 858
一整个雨季
一整个雨季 2021-02-13 21:39

First I create a serial queue like this

static dispatch_queue_t queue = dispatch_queue_create(\"myQueue\", DISPATCH_QUEUE_SERIAL);

then, at som

3条回答
  •  误落风尘
    2021-02-13 21:57

    With Davids answer getting me on track I succeeded in doing this like so

    taskCounter++;
    dispatch_async(queue, ^{
        if (taskCounter > 1) {
            taskCounter--;
            NSLog(@"%@", @"skip");
            return;
        }
        NSLog(@"%@", @"start");
        // do stuff
        sleep(3);
        taskCounter--;
        NSLog(@"%@", @"done");
    });
    

    taskCounter has to be either an ivar or a property (initialize it with 0). In that case it doesn't even need the __block attribute.

提交回复
热议问题