I'm trying to learn GCD and reading 
http://developer.apple.com/library/mac/#featuredarticles/BlocksGCD/index.html
But the Global Concurrent Queues example in that document (last updated 
2010-11-10) doesn't even compile. It complains about accessing array 'result' 
from within the block:

#define COUNT 128
__block double result[COUNT];
dispatch_apply(COUNT, q_default, ^(size_t i){
        result[i] = complex_calculation(i);   /* <=== ERROR HERE!
 });
double sum = 0;
for (int i=0; i < COUNT; i++) sum += result[i];


What is wrong here? This looks like it should work. Below is my full program.
-Jonathon


#include <dispatch/dispatch.h>
#include <stdio.h>
#define COUNT 128

double complex_calculation( int i )
{
  return i * 3.14;
}

int main()
{
  int i = 0;
  __block double result[COUNT];

  dispatch_queue_t q_default;
  /* get default queue */
  q_default = dispatch_get_global_queue(0, 0);

  dispatch_apply(COUNT, q_default, ^(size_t i) {
    result[i] = complex_calculation(i);   /* <=== ERROR HERE!
  });

  double sum = 0;
  for (i=0; i < COUNT; i++) sum += result[i];
  printf("%f\n", sum);
  return 0;
}

$ cc -g blk3.c -o blk3
blk3.c: In function ‘__main_block_invoke_1’:
blk3.c:20: error: cannot access __block variable of array type inside block


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to