Intel® oneAPI Threading Building Blocks Developer Guide and API Reference
ID
772616
Date
3/22/2024
Public
A newer version of this document is available. Customers should click here to go to the newest version.
Package Contents
Parallelizing Simple Loops
Parallelizing Complex Loops
Parallelizing Data Flow and Dependence Graphs
Work Isolation
Exceptions and Cancellation
Containers
Mutual Exclusion
Timing
Memory Allocation
The Task Scheduler
Design Patterns
Migrating from Threading Building Blocks (TBB)
Constrained APIs
Invoke a Callable Object
Appendix A Costs of Time Slicing
Appendix B Mixing With Other Threading Packages
References
parallel_for_each Body semantics and requirements
parallel_sort ranges interface extension
TBB_malloc_replacement_log Function
Type-specified message keys for join_node
Scalable Memory Pools
Helper Functions for Expressing Graphs
concurrent_lru_cache
task_group extensions
The customizing mutex type for concurrent_hash_map
Cancellation Without An Exception
To cancel an algorithm but not throw an exception, use the expression current_context()->cancel_group_execution(). The part current_context() references the task_group_context* of the currently executing task if any on the current thread. Calling cancel_group_execution() cancels all tasks in its task_group_context, which is explained in more detail in Cancellation and Nested Parallelism. The method returns true if it actually causes cancellation, false if the task_group_context was already cancelled.
The example below shows how to use current_context()->cancel_group_execution().
#include "oneapi/tbb.h"
#include <vector>
#include <iostream>
using namespace oneapi::tbb;
using namespace std;
vector<int> Data;
struct Update {
void operator()( const blocked_range<int>& r ) const {
for( int i=r.begin(); i!=r.end(); ++i )
if( i<Data.size() ) {
++Data[i];
} else {
// Cancel related tasks.
if( current_context()->cancel_group_execution() )
cout << "Index " << i << " caused cancellation\n";
return;
}
}
};
int main() {
Data.resize(1000);
parallel_for( blocked_range<int>(0, 2000), Update());
return 0;
}