main
test-branch
|
Last change
on this file since bb03188 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago |
|
Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.
git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c
|
-
Property mode
set to
100644
|
|
File size:
756 bytes
|
| Line | |
|---|
| 1 | #include <omp.h>
|
|---|
| 2 | #include <stdio.h>
|
|---|
| 3 |
|
|---|
| 4 | int B=2;
|
|---|
| 5 | int n=2;
|
|---|
| 6 | int forks[2];
|
|---|
| 7 | static omp_lock_t lock[2];
|
|---|
| 8 |
|
|---|
| 9 | void dine(int id){
|
|---|
| 10 | int left = id;
|
|---|
| 11 | int right = (id + 1) % n;
|
|---|
| 12 | while(1){
|
|---|
| 13 | omp_set_lock(&(lock[left]));
|
|---|
| 14 | if (forks[left]){
|
|---|
| 15 | forks[left] = 0;
|
|---|
| 16 | }
|
|---|
| 17 | omp_set_lock(&(lock[right]));
|
|---|
| 18 | if (forks[right]){
|
|---|
| 19 | forks[right] = 0;
|
|---|
| 20 | }
|
|---|
| 21 | forks[right] = 1;
|
|---|
| 22 | forks[left] = 1;
|
|---|
| 23 | omp_unset_lock(&(lock[right]));
|
|---|
| 24 | omp_unset_lock(&(lock[left]));
|
|---|
| 25 | }
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | int main(){
|
|---|
| 29 | omp_init_lock(&(lock[0]));
|
|---|
| 30 | omp_init_lock(&(lock[1]));
|
|---|
| 31 |
|
|---|
| 32 | for(int j=0; j<n; j++)
|
|---|
| 33 | forks[j] = 1;
|
|---|
| 34 | #pragma omp parallel
|
|---|
| 35 | {
|
|---|
| 36 | #pragma omp for
|
|---|
| 37 | for(int i=0; i<n; i++)
|
|---|
| 38 | dine(i);
|
|---|
| 39 | }
|
|---|
| 40 | omp_destroy_lock(&(lock[1]));
|
|---|
| 41 | omp_destroy_lock(&(lock[0]));
|
|---|
| 42 | } |
|---|
Note:
See
TracBrowser
for help on using the repository browser.