13.11.2016 Views

OpenMP Application Programming Interface Examples

2fZ58Wr

2fZ58Wr

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1<br />

2<br />

3<br />

4<br />

6.9.5 Nestable Lock Routines<br />

The following example demonstrates how a nestable lock can be used to synchronize updates both<br />

to a whole structure and to one of its members.<br />

C / C++<br />

Example nestable_lock.1.c<br />

S-1 #include <br />

S-2 typedef struct {<br />

S-3 int a,b;<br />

S-4 omp_nest_lock_t lck; } pair;<br />

S-5<br />

S-6 int work1();<br />

S-7 int work2();<br />

S-8 int work3();<br />

S-9 void incr_a(pair *p, int a)<br />

S-10 {<br />

S-11 /* Called only from incr_pair, no need to lock. */<br />

S-12 p->a += a;<br />

S-13 }<br />

S-14 void incr_b(pair *p, int b)<br />

S-15 {<br />

S-16 /* Called both from incr_pair and elsewhere, */<br />

S-17 /* so need a nestable lock. */<br />

S-18<br />

S-19 omp_set_nest_lock(&p->lck);<br />

S-20 p->b += b;<br />

S-21 omp_unset_nest_lock(&p->lck);<br />

S-22 }<br />

S-23 void incr_pair(pair *p, int a, int b)<br />

S-24 {<br />

S-25 omp_set_nest_lock(&p->lck);<br />

S-26 incr_a(p, a);<br />

S-27 incr_b(p, b);<br />

S-28 omp_unset_nest_lock(&p->lck);<br />

S-29 }<br />

S-30 void nestlock(pair *p)<br />

S-31 {<br />

S-32 #pragma omp parallel sections<br />

S-33 {<br />

S-34 #pragma omp section<br />

S-35 incr_pair(p, work1(), work2());<br />

S-36 #pragma omp section<br />

S-37 incr_b(p, work3());<br />

S-38 }<br />

S-39 }<br />

206 <strong>OpenMP</strong> <strong>Examples</strong> Version 4.5.0 - November 2016

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!