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>
|
||||
class elem_array : private elem_array_head {
|
||||
struct head_t {
|
||||
std::atomic<std::size_t> rc_ { 0 }; // read counter
|
||||
std::atomic<std::uint32_t> rc_ { 0 }; // read counter
|
||||
};
|
||||
|
||||
public:
|
||||
@ -46,8 +46,6 @@ public:
|
||||
block_size = elem_size * elem_max
|
||||
};
|
||||
|
||||
static_assert(data_size % alignof(head_t) == 0, "data_size must be multiple of alignof(head_t)");
|
||||
|
||||
private:
|
||||
struct elem_t {
|
||||
head_t head_;
|
||||
@ -101,10 +99,10 @@ public:
|
||||
elem_t* el = elem(wt_.load(std::memory_order_acquire));
|
||||
// check all consumers have finished reading
|
||||
while(1) {
|
||||
std::size_t expected = 0;
|
||||
std::uint32_t expected = 0;
|
||||
if (el->head_.rc_.compare_exchange_weak(
|
||||
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)) {
|
||||
break;
|
||||
}
|
||||
|
||||
@ -58,15 +58,16 @@ void test_prod_cons(void) {
|
||||
int dat_;
|
||||
};
|
||||
|
||||
std::unordered_map<int, std::vector<int>> list[std::extent<decltype(consumers)>::value];
|
||||
int cid = 0;
|
||||
for (auto& t : consumers) {
|
||||
t = std::thread{[&] {
|
||||
t = std::thread{[&, cid] {
|
||||
auto cur = cq__->cursor();
|
||||
std::cout << "start consumer " << &t << ": cur = " << (int)cur << std::endl;
|
||||
|
||||
cq__->connect();
|
||||
std::unique_ptr<cq_t, void(*)(cq_t*)> guard(cq__, [](cq_t* cq) { cq->disconnect(); });
|
||||
|
||||
std::unordered_map<int, std::vector<int>> list;
|
||||
do {
|
||||
while (cur != cq__->cursor()) {
|
||||
msg_t* pmsg = static_cast<msg_t*>(cq__->take(cur)),
|
||||
@ -74,7 +75,7 @@ void test_prod_cons(void) {
|
||||
cq__->put(pmsg);
|
||||
if (msg.pid_ < 0) goto finished;
|
||||
++cur;
|
||||
list[msg.pid_].push_back(msg.dat_);
|
||||
list[cid][msg.pid_].push_back(msg.dat_);
|
||||
}
|
||||
} while(1);
|
||||
finished:
|
||||
@ -82,10 +83,10 @@ void test_prod_cons(void) {
|
||||
auto ts = sw.elapsed<std::chrono::microseconds>();
|
||||
std::cout << "[" << N << ":" << M << ", " << Loops << "]" << std::endl
|
||||
<< "performance: " << (double(ts) / double(Loops * N)) << " us/d" << 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) {
|
||||
auto& vec = list[n];
|
||||
auto& vec = cons_vec[n];
|
||||
QCOMPARE(vec.size(), static_cast<std::size_t>(Loops));
|
||||
int i = 0;
|
||||
for (int d : vec) {
|
||||
@ -93,7 +94,10 @@ void test_prod_cons(void) {
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}};
|
||||
++cid;
|
||||
}
|
||||
|
||||
while (cq__->conn_count() != std::extent<decltype(consumers)>::value) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user