简体   繁体   English

使块在串行队列中等待

[英]Making a block wait in a serial queue

I am using a serial queue in GCD like 我在GCD中使用串行队列,例如

dispatch_queue_t myCustomQueue;
myCustomQueue = dispatch_queue_create("com.example.MyCustomQueue", NULL);

dispatch_async(myCustomQueue, ^{
    ....
/* some asyncronus call*/

});

in the block i want to make a asynchronous call like playing a audio, 在该块中,我想拨打一个异步电话,例如播放音频,

My main problem is as soon as i am making a asynchronous call the block is not waiting and it completes the execution of whole block. 我的主要问题是,一旦我进行异步调用,该块就不会等待,它会完成整个块的执行。

Can i make the block wait for some time until some callback from the asynchronous call returns 我可以让该块等待一段时间,直到异步调用返回一些回调

The block cannot wait, but the thread who is exceuting it can. 不能等待,但是执行它的线程可以。

Look into NSCondition which enables you to wait and signal threads 查看NSCondition ,它使您可以wait和发signal线程

Basically you allocate a NSCondition and in the block you'll have [condition wait]; 基本上,您分配一个NSCondition并在该块中具有[condition wait]; which will cause the thread to wait. 这将导致线程等待。 then, when the async operation is done, you call [condition signal]; 然后,当异步操作完成后,您调用[condition signal]; which will signal the waiting thread to continue. 这将通知等待线程继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM