Skip to content

Instantly share code, notes, and snippets.

@kakysha
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakysha/c125f96f9bd3e506ce16 to your computer and use it in GitHub Desktop.
Save kakysha/c125f96f9bd3e506ce16 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <unistd.h>
#include <ctime>
#include <unordered_map>
#include <cds/init.h>
#include <cds/gc/hp.h>
#include "cds/opt/hash.h"
#include "cds/container/michael_list_hp.h"
#include "cds/container/split_list_map.h"
namespace cc = cds::container;
using namespace std;
typedef cc::SplitListMap<
cds::gc::HP,
string,
long,
cc::split_list::make_traits< // metafunction to build split-list traits
cc::split_list::ordered_list<cc::michael_list_tag>, // tag for underlying ordered list implementation
cc::opt::hash< std::hash<string> >, // hash functor
cc::split_list::ordered_list_traits< // ordered list traits desired
cc::michael_list::make_traits< // metafunction to build lazy list traits
cc::opt::less< std::less<string> > // less-based compare functor
>::type
>
>::type> string_int_map;
const int NUM_ELEMS = 1000000;
void async_insert(int tind, string_int_map& map) {
cds::threading::Manager::attachThread();
while (true) {
for (int i = tind*NUM_ELEMS; i < (tind+1)*NUM_ELEMS; i++) {
map.insert(to_string(i), i);
}
sleep(1);
}
cds::threading::Manager::detachThread();
}
void async_delete(int tind, string_int_map& map) {
cds::threading::Manager::attachThread();
while (true) {
for (int i = tind*NUM_ELEMS; i < (tind+1)*NUM_ELEMS; i++) {
map.erase(to_string(i));
}
sleep(1);
}
cds::threading::Manager::detachThread();
}
int main(void) {
cds::Initialize();
{
cds::gc::HP hpGC;
cds::threading::Manager::attachThread();
string_int_map sessions;
std::thread t;
for (int i =0; i <10; i++) {
t = i % 2 == 0 ? std::thread(async_insert, 1, std::ref(sessions))
: std::thread(async_delete, 1, std::ref(sessions));
t.detach();
}
while (true) sleep(1);
}
cds::Terminate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment