Created
December 7, 2017 09:29
-
-
Save theidexisted/2461c2e86519272933bdcdbc0a60eb4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
default: | |
g++ -O0 -ggdb3 -std=c++11 -o example -lcds_d main.cpp -pthread | |
clear: | |
rm example |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <atomic> | |
#include <functional> | |
#include <iostream> | |
#include <thread> | |
#include <vector> | |
#include <cds/init.h> | |
#include <cds/threading/model.h> | |
#include <cds/urcu/details/gpb.h> | |
#include <cds/urcu/general_buffered.h> | |
struct Scope { | |
using fun_t = std::function<void()>; | |
Scope(fun_t &&ct = nullptr, fun_t &&dt = nullptr) : ct_(ct), dt_(dt) { | |
if (ct_) { | |
ct_(); | |
} | |
} | |
~Scope() { | |
if (dt_) { | |
dt_(); | |
} | |
} | |
fun_t ct_; | |
fun_t dt_; | |
}; | |
struct Dummy { | |
Dummy(int a, int b) : a(a), b(b) {} | |
int a; | |
int b; | |
char placeholder[(4096 - sizeof(int) * 2) * 10]; | |
}; | |
int main(int argc, char *argv[]) { | |
struct CDS_Register { | |
CDS_Register() { cds::threading::Manager::attachThread(); } | |
~CDS_Register() { cds::threading::Manager::detachThread(); } | |
}; | |
std::atomic<Dummy *> pval = {new Dummy{0, 0}}; | |
auto deleter = [](void *p) { | |
auto pairp = static_cast<Dummy *>(p); | |
// std::cout << "now deleting prt" << std::endl; | |
delete pairp; | |
}; | |
cds::Initialize(); | |
{ | |
using buffered_rcu_t = cds::urcu::gc<cds::urcu::general_buffered<>>; | |
buffered_rcu_t gbRCU; | |
CDS_Register reg; | |
auto readers = [&](int tid) { | |
CDS_Register reg; | |
while (true) { | |
buffered_rcu_t::scoped_lock lk; | |
auto p = pval.load(); | |
auto first = p->a; | |
auto second = p->b; | |
if (first != second) { | |
std::cout << "tid:" << tid << " value not equal:" << first << "," | |
<< second << std::endl; | |
} | |
std::this_thread::sleep_for(std::chrono::microseconds(10)); | |
} | |
}; | |
auto writers = [&]() { | |
CDS_Register reg; | |
int i = 0; | |
while (true) { | |
std::this_thread::sleep_for(std::chrono::milliseconds(10)); | |
{ | |
int v = std::rand(); | |
buffered_rcu_t::retire_ptr(pval.load(), deleter); | |
pval.store(new Dummy{v, v}); | |
} | |
} | |
}; | |
std::vector<std::thread> thr_vec; | |
for (int i = 0; i < 16; ++i) { | |
thr_vec.emplace_back(std::thread(readers, i)); | |
} | |
thr_vec.emplace_back(std::thread(writers)); | |
for (auto &t : thr_vec) { | |
t.join(); | |
} | |
} | |
cds::Terminate(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment