objective-c - Grand Central Dispatch 的简单示例

标签 objective-c macos grand-central-dispatch

我是 Mac 编程新手,我对 Grand Central Dispatch 感到非常惊讶。我读到了它,看起来像是并行编程的完美解决方案。我使用 POSIX 线程并想转移到 GCD。

我在 Apple Developer Connection 中看到了示例代码,但它让我很困惑。我搜索了一个包含两个线程的简单示例,但找不到。

如何使用 GCD 编写此示例代码???

#include <stdio.h>       /* standard I/O routines                 */
#include <pthread.h>     /* pthread functions and data structures */

/* function to be executed by the new thread */
void* do_loop(void* data)
{
int i;          /* counter, to print numbers */
int j;          /* counter, for delay        */
int me = *((int*)data);     /* thread identifying number */

for (i=0; i<10; i++) 
{
    for (j=0; j<500000; j++) /* delay loop */
    ;
    printf("'%d' - Got '%d'\n", me, i);
}

/* terminate the thread */
pthread_exit(NULL);
}
void* th2(void* data)
{
cout << "Thread nº 2" << endl;
}

int main(int argc, char* argv[])
{
int        thr_id;         /* thread ID for the newly created thread */
pthread_t  p_thread1;
pthread_t  p_thread2;       /* thread's structure                     */
int        a         = 1;  /* thread 1 identifying number            */
int        b         = 2;  /* thread 2 identifying number            */

/* create a new thread that will execute 'do_loop()' */
thr_id = pthread_create(&p_thread1, NULL, do_loop, (void*)&a);
/* run 'do_loop()' in the main thread as well */
thr_id = pthread_create(&p_thread2, NULL, th2, (void*)&b);


return 0;
}

提前致谢

最佳答案

应该是这样的:

// The block replaces your doLoop function, it basically does the same thing
dispatch_block_t myBlock = ^{
    int i;          /* counter, to print numbers */
    int j;          /* counter, for delay        */

    dispatch_queue_t me = dispatch_get_current_queue();     /* The queue which currently runs this block */

    for (i=0; i<10; i++) 
    {
        for (j=0; j<500000; j++) /* delay loop */
            ;


        printf("'%s' - Got '%d'\n", dispatch_queue_get_label(me), i); // Print the name of the queue
    }
};


// Create two queues
dispatch_queue_t queue1 = dispatch_queue_create("my.totally.unique.and.reverse.dns.identifier.1", NULL);
dispatch_queue_t queue2 = dispatch_queue_create("my.totally.unique.and.reverse.dns.identifier.2", NULL);

// Let both execute the block
dispatch_async(queue1, myBlock);
dispatch_async(queue2, myBlock);

// And then release the queues because we are great citizens who care about memory
dispatch_release(queue1);
dispatch_release(queue2);

关于objective-c - Grand Central Dispatch 的简单示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5502906/

相关文章:

objective-c - 如何使 Swift 的协议(protocol)在 .h 文件中可见?

iphone - 我是否正确使用 CNCopyCurrentNetworkInfo?

iphone - 如何选择注释并将 id 传递到特定的详细信息页面?

ios - 每次我关闭并重新打开 Xcode 时,xcode 9.3 session 都会过期

macos - 如何让 Mac OS 在特定地址范围内分配内存?

ios - dispatch_async 是否可以中断调用任务

objective-c - 我怎样才能让一个方法停止一段固定的时间?

mysql - Xampp 无法在 Mac OSX 上启动 MySQL 服务器?

c - DispatchReadSource 事件处理程序未针对绑定(bind)套接字触发

ios - 在 Swift 中创建 GCD 队列?