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:
1004 bytes
|
| Line | |
|---|
| 1 | /*
|
|---|
| 2 | * Modified from OpenMP API Examples Ver.4.5.0 (Nov. 2016):
|
|---|
| 3 | * Sec. 6.9.4 Simple Lock Routines Pg. 204
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | #include <civlc.cvh>
|
|---|
| 7 | #include <stdio.h>
|
|---|
| 8 | #include <omp.h>
|
|---|
| 9 |
|
|---|
| 10 | void skip(int i) {
|
|---|
| 11 | printf("[TID: %d]: Failed to test the lock.\n", i);
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | void work(int i) {
|
|---|
| 15 | printf("[TID: %d]: Succed to test the lock.\n", i);
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | int main() {
|
|---|
| 19 | omp_lock_t lck;
|
|---|
| 20 | int id, data = -1;
|
|---|
| 21 | omp_init_lock(&lck);
|
|---|
| 22 |
|
|---|
| 23 | #pragma omp parallel shared(lck, data) private(id)
|
|---|
| 24 | {
|
|---|
| 25 | id = data;
|
|---|
| 26 | id = omp_get_thread_num();
|
|---|
| 27 |
|
|---|
| 28 | omp_set_lock(&lck);
|
|---|
| 29 | #ifdef BAD
|
|---|
| 30 | data = 100;
|
|---|
| 31 | #endif
|
|---|
| 32 | /* only one thread at a time can execute this printf */
|
|---|
| 33 | printf("My thread id is %d.\n", id);
|
|---|
| 34 | omp_unset_lock(&lck);
|
|---|
| 35 |
|
|---|
| 36 | while (! omp_test_lock(&lck)) {
|
|---|
| 37 | skip(id); /* we do not yet have the lock,
|
|---|
| 38 | so we must do something else */
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | work(id); /* we now have the lock
|
|---|
| 42 | and can do the work */
|
|---|
| 43 |
|
|---|
| 44 | omp_unset_lock(&lck);
|
|---|
| 45 | }
|
|---|
| 46 | omp_destroy_lock(&lck);
|
|---|
| 47 |
|
|---|
| 48 | return 0;
|
|---|
| 49 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.