Skip to content

Instantly share code, notes, and snippets.

@OlegJakushkin
Created July 27, 2015 15:46
Show Gist options
  • Save OlegJakushkin/bb2048f54e7f1a038a1f to your computer and use it in GitHub Desktop.
Save OlegJakushkin/bb2048f54e7f1a038a1f to your computer and use it in GitHub Desktop.
// icpc -std=c++11 -ltbb -lpthread -lrt -O3 -mtune=native -march=native -test_tbb.cpp -o exec
#include <tbb/concurrent_hash_map.h>
#include <tbb/tick_count.h>
#include <tbb/tbb_thread.h>
#include <mutex>
#include <memory>
#include <map>
#include <atomic>
#include <vector>
#include <iostream>
#include <chrono>
#include <thread>
struct data {
int d;
data():d(666){}
~data(){
//std::cout << d << " : destructor called!";
}
};
// For user on response handling
struct TBBtest {
tbb::concurrent_hash_map<int, std::shared_ptr<data>> handlers;
virtual void OnAction(const int & id) {
//std::cout << "OnAction started" << std::endl;
tbb::concurrent_hash_map<int, std::shared_ptr<data>>::accessor callback;
if (handlers.find(callback ,id)) {
//std::cout << "OnAction callback found!" << std::endl;
// std::cout << callback->second->d << std::endl;
handlers.erase(callback);
}
}
void AddHandler(const int &id, std::shared_ptr<data> handler) {
tbb::concurrent_hash_map<int, std::shared_ptr<data>>::accessor callback;
if (handlers.find(callback ,id)) {
callback->second = handler;
} else {
handlers.insert(callback, id);
callback->second = handler;
}
callback.release();
}
void RemoveHandler(const int &id) {
tbb::concurrent_hash_map<int, std::shared_ptr<data>>::accessor callback;
if (handlers.find(callback ,id)) {
handlers.erase(callback);
}
}
};
struct scoped_timer {
tbb::tick_count t1;
scoped_timer() {
t1 = tbb::tick_count::now();
}
~scoped_timer() {
auto t2 = tbb::tick_count::now();
auto millis = (t2 - t1).seconds();
std::cout << millis << std::endl;
}
};
int tmain(){
TBBtest t;
std::atomic<int> counter = 0;
auto test1 = [&]() {
scoped_timer time;
counter++;
for (auto i = 0; i < 3999999; i++) {
t.OnAction(i);
}
counter--;
};
auto test2 = [&]() {
scoped_timer time;
counter++;
for (auto i = 3999998; i >= 0; i--) {
t.OnAction(i);
}
counter--;
};
auto test3 = [&]() {
scoped_timer time;
counter++;
for (auto i = 0; i < 3999999; i++) {
t.OnAction(rand()%3999999);
}
counter--;
};
auto testFill = [&](){
std::cout << "refilig tests" << std::endl;
scoped_timer time;
counter++;
for (auto i = 0; i < 3999999; i++) {
auto d = std::make_shared<data>();
d->d = i + 1;
t.AddHandler(i, d);
}
counter--;
};
std::cout << "created lambdas... starting tests" << std::endl;
for(auto j = 0; j < 10 ; j++) {
testFill();
std::vector<std::shared_ptr<std::thread>> v;
for(int k = 0; k < j+1; k++) {
if(k==j) {
v.push_back(std::make_shared<std::thread>(testFill));
} else {
v.push_back(std::make_shared<std::thread>(test3));
}
}
std::cout << "all tests started" << std::endl;
while(counter != 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
std::cout << "main thread waiting ended! for " << j << " tests." << std::endl;
for(auto & t : v) {t->join();}
}
std::cout<< "all tests ended!" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment