mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
cross-platform
This commit is contained in:
parent
da25b7fc81
commit
c7f49fad5a
@ -34,7 +34,7 @@ enum : std::size_t {
|
|||||||
template <std::size_t DataSize>
|
template <std::size_t DataSize>
|
||||||
class elem_array : private elem_array_head {
|
class elem_array : private elem_array_head {
|
||||||
struct head_t {
|
struct head_t {
|
||||||
std::atomic<std::size_t> rc_ { 0 }; // read counter
|
std::atomic<std::uint32_t> rc_ { 0 }; // read counter
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -46,8 +46,6 @@ public:
|
|||||||
block_size = elem_size * elem_max
|
block_size = elem_size * elem_max
|
||||||
};
|
};
|
||||||
|
|
||||||
static_assert(data_size % alignof(head_t) == 0, "data_size must be multiple of alignof(head_t)");
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct elem_t {
|
struct elem_t {
|
||||||
head_t head_;
|
head_t head_;
|
||||||
@ -101,10 +99,10 @@ public:
|
|||||||
elem_t* el = elem(wt_.load(std::memory_order_acquire));
|
elem_t* el = elem(wt_.load(std::memory_order_acquire));
|
||||||
// check all consumers have finished reading
|
// check all consumers have finished reading
|
||||||
while(1) {
|
while(1) {
|
||||||
std::size_t expected = 0;
|
std::uint32_t expected = 0;
|
||||||
if (el->head_.rc_.compare_exchange_weak(
|
if (el->head_.rc_.compare_exchange_weak(
|
||||||
expected,
|
expected,
|
||||||
static_cast<std::size_t>(cc_.load(std::memory_order_relaxed)),
|
static_cast<std::uint32_t>(cc_.load(std::memory_order_relaxed)),
|
||||||
std::memory_order_release)) {
|
std::memory_order_release)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -58,15 +58,16 @@ void test_prod_cons(void) {
|
|||||||
int dat_;
|
int dat_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::unordered_map<int, std::vector<int>> list[std::extent<decltype(consumers)>::value];
|
||||||
|
int cid = 0;
|
||||||
for (auto& t : consumers) {
|
for (auto& t : consumers) {
|
||||||
t = std::thread{[&] {
|
t = std::thread{[&, cid] {
|
||||||
auto cur = cq__->cursor();
|
auto cur = cq__->cursor();
|
||||||
std::cout << "start consumer " << &t << ": cur = " << (int)cur << std::endl;
|
std::cout << "start consumer " << &t << ": cur = " << (int)cur << std::endl;
|
||||||
|
|
||||||
cq__->connect();
|
cq__->connect();
|
||||||
std::unique_ptr<cq_t, void(*)(cq_t*)> guard(cq__, [](cq_t* cq) { cq->disconnect(); });
|
std::unique_ptr<cq_t, void(*)(cq_t*)> guard(cq__, [](cq_t* cq) { cq->disconnect(); });
|
||||||
|
|
||||||
std::unordered_map<int, std::vector<int>> list;
|
|
||||||
do {
|
do {
|
||||||
while (cur != cq__->cursor()) {
|
while (cur != cq__->cursor()) {
|
||||||
msg_t* pmsg = static_cast<msg_t*>(cq__->take(cur)),
|
msg_t* pmsg = static_cast<msg_t*>(cq__->take(cur)),
|
||||||
@ -74,7 +75,7 @@ void test_prod_cons(void) {
|
|||||||
cq__->put(pmsg);
|
cq__->put(pmsg);
|
||||||
if (msg.pid_ < 0) goto finished;
|
if (msg.pid_ < 0) goto finished;
|
||||||
++cur;
|
++cur;
|
||||||
list[msg.pid_].push_back(msg.dat_);
|
list[cid][msg.pid_].push_back(msg.dat_);
|
||||||
}
|
}
|
||||||
} while(1);
|
} while(1);
|
||||||
finished:
|
finished:
|
||||||
@ -82,18 +83,21 @@ void test_prod_cons(void) {
|
|||||||
auto ts = sw.elapsed<std::chrono::microseconds>();
|
auto ts = sw.elapsed<std::chrono::microseconds>();
|
||||||
std::cout << "[" << N << ":" << M << ", " << Loops << "]" << std::endl
|
std::cout << "[" << N << ":" << M << ", " << Loops << "]" << std::endl
|
||||||
<< "performance: " << (double(ts) / double(Loops * N)) << " us/d" << std::endl;
|
<< "performance: " << (double(ts) / double(Loops * N)) << " us/d" << std::endl;
|
||||||
}
|
std::cout << "confirming..." << std::endl;
|
||||||
std::cout << "confirming..." << std::endl;
|
for (auto& cons_vec : list) {
|
||||||
for (int n = 0; n < static_cast<int>(std::extent<decltype(producers)>::value); ++n) {
|
for (int n = 0; n < static_cast<int>(std::extent<decltype(producers)>::value); ++n) {
|
||||||
auto& vec = list[n];
|
auto& vec = cons_vec[n];
|
||||||
QCOMPARE(vec.size(), static_cast<std::size_t>(Loops));
|
QCOMPARE(vec.size(), static_cast<std::size_t>(Loops));
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (int d : vec) {
|
for (int d : vec) {
|
||||||
QCOMPARE(i, d);
|
QCOMPARE(i, d);
|
||||||
++i;
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}};
|
}};
|
||||||
|
++cid;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (cq__->conn_count() != std::extent<decltype(consumers)>::value) {
|
while (cq__->conn_count() != std::extent<decltype(consumers)>::value) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user