Простой пример of 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;
}

Заранее спасибо

11
задан Brad Larson 19 December 2011 в 20:12
поделиться