开发者

dispatch_async() in c

I am having problems understanding GCD. i need to use dispatch_async to spawn the function put_values() which will in an endless loop put values into a buffer. Get_values() will remove then also in an endless loop. I therefore have to run them at the same time without order to see if i implemented my semaphores for waiting correctly. WIll the code below do that (running them asynchronously ) ? Thank yo开发者_如何学Cu !

dispatch_queue_t producer = dispatch_queue_create("producer", NULL);
dispatch_queue_t consumer = dispatch_queue_create("consumer", NULL);

dispatch_async(producer, 
               ^{
                   put_values();
               });

dispatch_async(consumer, 
               ^{
                   get_values();
               });

dispatch_main();


Your code should do exactly that, unless I am missing something. Because you are using two different queues they should both run at the same time.


The question suggests some confusion about what grand central dispatch actually does. Stop thinking in terms of threads or "concurrency" as some big picture goal and start thinking instead about what operations need to be synchronous and which can be asynchronous and you (and many of the folks who answered) will begin to understand how to factor this properly. For one thing, your readers should be synchronous and your writers should be asynchronous, each value to be read/written having a serial queue associated with it. How the system then parallelizes this with respect to other serial queues (or the read/write requests themselves) is not something you should or need to care about.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜