1.. _appendix_B:
2
3Appendix B Mixing With Other Threading Packages
4===============================================
5
6
7|full_name| can be mixed with other
8threading packages. No special effort is required to use any part of
9oneTBB with other threading packages.
10
11
12Here is an example that parallelizes an outer loop with OpenMP and an
13inner loop with oneTBB.
14
15
16::
17
18
19   int M, N;
20    
21
22   struct InnerBody {
23       ...
24   };
25    
26
27   void TBB_NestedInOpenMP() {
28   #pragma omp parallel
29       {
30   #pragma omp for
31           for( int i=0; i<M; ++ ) {
32               parallel_for( blocked_range<int>(0,N,10), InnerBody(i) );
33           }
34       }
35   }
36
37
38The details of ``InnerBody`` are omitted for brevity. The
39``#pragma omp parallel`` causes the OpenMP to create a team of threads,
40and each thread executes the block statement associated with the pragma.
41The ``#pragma omp for`` indicates that the compiler should use the
42previously created thread team to execute the loop in parallel.
43
44
45Here is the same example written using POSIX\* Threads.
46
47
48::
49
50
51   int M, N;
52    
53
54   struct InnerBody {
55       ...
56   };
57    
58
59   void* OuterLoopIteration( void* args ) {
60       int i = (int)args;
61       parallel_for( blocked_range<int>(0,N,10), InnerBody(i) );
62   }
63    
64
65   void TBB_NestedInPThreads() {
66       std::vector<pthread_t> id( M );
67       // Create thread for each outer loop iteration
68       for( int i=0; i<M; ++i )
69           pthread_create( &id[i], NULL, OuterLoopIteration, NULL );
70       // Wait for outer loop threads to finish
71       for( int i=0; i<M; ++i )
72           pthread_join( &id[i], NULL );
73   }
74
75