forked from OpenMP/Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample_cancellation.1.cpp
More file actions
48 lines (45 loc) · 1.07 KB
/
Example_cancellation.1.cpp
File metadata and controls
48 lines (45 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* @@name: cancellation.1c
* @@type: C++
* @@compilable: yes
* @@linkable: no
* @@expect: success
*/
#include <iostream>
#include <exception>
#include <cstddef>
#define N 10000
extern void causes_an_exception();
extern void phase_1();
extern void phase_2();
void example() {
std::exception *ex = NULL;
#pragma omp parallel shared(ex)
{
#pragma omp for
for (int i = 0; i < N; i++) {
// no 'if' that prevents compiler optimizations
try {
causes_an_exception();
}
catch (std::exception *e) {
// still must remember exception for later handling
#pragma omp atomic write
ex = e;
// cancel worksharing construct
#pragma omp cancel for
}
}
// if an exception has been raised, cancel parallel region
if (ex) {
#pragma omp cancel parallel
}
phase_1();
#pragma omp barrier
phase_2();
}
// continue here if an exception has been thrown in the worksharing loop
if (ex) {
// handle exception stored in ex
}
}