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;
}
提前致谢
I'm newbie programming for mac and i'm really surprised on Grand Central Dispatch. I read about that and looks like the perfect solution for parallel programming. I worked with POSIX threads and want to move to GCD.
I saw the samples codes in the Apple Developer Connection, but It confused me so much. I searched for an easy example with two threads to start but i can't find it.
How can I do this sample code using 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;
}
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它会是这样的:
It would be something like this:
只需将需要异步运行的代码包装在传递给
dispatch_async()
的块中即可:请注意,在此示例和您的示例中,应用程序实际上都会立即返回,因为您没有采取任何措施来延迟 <代码>main()。
Just wrap the code that needs to run asynchronously in a block passed to
dispatch_async()
:Note that in both this example and yours, the app will actually return immediately since you do nothing to defer the return from
main()
.