安装
Ubuntu:
sudo apt-get install libtbb2 libtbb-dev libtbb-docMacOS上:
sudo port install tbb编译
g++ -ltbb foo.cpp使用
concurrent_hash_map
此容器是类似于STL中的map的存在. 同std::map一样,concurrent_hash_map
#include <iostream> #include <string> #include <tbb/concurrent_hash_map.h> using namespace tbb; using namespace std; typedef concurrent_hash_map<string,string> CacheTable; int main() { CacheTable cache; // insert an element to the map CacheTable::accessor a; cache.insert(a, s); a->second = "value1"; a.release(); // look for an element in the map CacheTable::const_accessor ca; if (cache.find(ca, "key1")) cout << "the value is " << ca->second << endl; else cout << "not found" << endl; // iterate over the map for(CacheTable::const_iterator itr=cache.begin(); itr!=cache.end(); ++itr) std::cout << itr->first << " - " << itr->second << std::endl; }concurrent_queue
一个类似于stl中queue的存在. 提供包括push(item),pop(item)以及try_pop(item)等等的操作.
下面是对于一个concurrent_queue的iteration操作:
#include <iostream> typedef concurrent_queue::const_iterator iter; for(iter i(q.unsafe_begin()); i!=q.unsafe_end(); ++i ) { do sth }
atomic
如果atomic< your data type> x, 以下操作为原子操作
= x read the value of x x = write the value of x, and return it x.fetch_and_store(y) do y=x and return the old value of x x.fetch_and_add(y) do x+=y and return the old value of x x.compare_and_swap(y,z) if x equals z, then do x=y. In either case, return old value of x
No comments:
Post a Comment