Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Grand Central Dispatch Programming
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="xstep" data-source="post: 1096975" data-attributes="member: 11647"><p>You had several problems with the code you posted, but I'm only going to talk about why you had trouble with sum.</p><p></p><p>First, the sum variable has to be placed outside the dispatch and declared with the __block keyword. This allows altering that variable from within the block. Also, given its new placement, you can now use it to get pi after the threading is complete.</p><p></p><p>Second, using the sum variable for an assignment to another variable before all threads have completed will likely return an invalid number because it hasn't had the final result assigned to it yet. To avoid that, look at the group functions I used.</p><p></p><p>Third, notice the new sumQueue. I used that because some other info I found suggested that it was good practice in this case, to not use the default queue. I quick check showed the default queue also works. So I'm not sure what is best.</p><p></p><p></p><p>[CODE]void doFixed()</p><p>{</p><p> NSLog(@"doFixed");</p><p> </p><p> NSInteger num_steps = 314;</p><p> </p><p> dispatch_queue_t theQueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);</p><p> dispatch_queue_t sumQueue = dispatch_queue_create("com.example.sum", NULL);</p><p> dispatch_group_t asyncGroup = dispatch_group_create();</p><p> </p><p> __block double sum = 0.0;</p><p> double step = 1.0/(double) num_steps;</p><p> double pi;</p><p> </p><p> dispatch_apply (num_steps, theQueue, ^ (size_t i) </p><p> {</p><p> double x;</p><p> x = (i+0.5)*step;</p><p> dispatch_group_async( asyncGroup, sumQueue,</p><p> ^{</p><p> sum = sum + 4.0/(1.0+x*x); </p><p> });</p><p></p><p> });</p><p> dispatch_group_wait(asyncGroup, DISPATCH_TIME_FOREVER);</p><p> dispatch_release(sumQueue);</p><p></p><p> pi = step * sum;</p><p> printf("The value of pi is %f\n", pi);</p><p>}</p><p>[/CODE]</p><p></p><p>By the way, when you post code, use the CODE wrapping tags, like I did. It makes it easier for people to read.</p></blockquote><p></p>
[QUOTE="xstep, post: 1096975, member: 11647"] You had several problems with the code you posted, but I'm only going to talk about why you had trouble with sum. First, the sum variable has to be placed outside the dispatch and declared with the __block keyword. This allows altering that variable from within the block. Also, given its new placement, you can now use it to get pi after the threading is complete. Second, using the sum variable for an assignment to another variable before all threads have completed will likely return an invalid number because it hasn't had the final result assigned to it yet. To avoid that, look at the group functions I used. Third, notice the new sumQueue. I used that because some other info I found suggested that it was good practice in this case, to not use the default queue. I quick check showed the default queue also works. So I'm not sure what is best. [CODE]void doFixed() { NSLog(@"doFixed"); NSInteger num_steps = 314; dispatch_queue_t theQueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_queue_t sumQueue = dispatch_queue_create("com.example.sum", NULL); dispatch_group_t asyncGroup = dispatch_group_create(); __block double sum = 0.0; double step = 1.0/(double) num_steps; double pi; dispatch_apply (num_steps, theQueue, ^ (size_t i) { double x; x = (i+0.5)*step; dispatch_group_async( asyncGroup, sumQueue, ^{ sum = sum + 4.0/(1.0+x*x); }); }); dispatch_group_wait(asyncGroup, DISPATCH_TIME_FOREVER); dispatch_release(sumQueue); pi = step * sum; printf("The value of pi is %f\n", pi); } [/CODE] By the way, when you post code, use the CODE wrapping tags, like I did. It makes it easier for people to read. [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Grand Central Dispatch Programming
Top