diff --git a/demo/chat/main.cpp b/demo/chat/main.cpp index 8065181..eb71573 100644 --- a/demo/chat/main.cpp +++ b/demo/chat/main.cpp @@ -1,37 +1,56 @@ #include #include #include +#include #include "ipc.h" +namespace ipc { +namespace detail { + +IPC_EXPORT std::size_t calc_unique_id(); + +} // namespace detail +} // namespace ipc + namespace { char name__[] = "ipc-chat"; char quit__[] = "q"; +char id__ [] = "c"; } // namespace int main() { - std::string buf; + std::string buf, id = id__ + std::to_string(ipc::detail::calc_unique_id()); + std::regex reg { "(c\\d+)> (.*)" }; + ipc::channel cc { name__ }; - std::thread r {[] { + std::thread r {[&id, ®] { ipc::channel cc { name__ }; + std::cout << id << " is ready." << std::endl; while (1) { auto buf = cc.recv(); if (buf.empty()) continue; std::string dat { static_cast(buf.data()), buf.size() - 1 }; - if (dat == quit__) { - std::cout << "receiver quit..." << std::endl; - return; + std::smatch mid; + if (std::regex_match(dat, mid, reg)) { + if (mid.str(1) == id) { + if (mid.str(2) == quit__) { + std::cout << "receiver quit..." << std::endl; + return; + } + continue; + } } - std::cout << "> " << dat << std::endl; + std::cout << dat << std::endl; } }}; while (1) { std::cin >> buf; - cc.send(buf); + cc.send(id + "> " + buf); if (buf == quit__) break; } diff --git a/src/platform/waiter_win.h b/src/platform/waiter_win.h index 3a09a9a..978dd3d 100644 --- a/src/platform/waiter_win.h +++ b/src/platform/waiter_win.h @@ -37,7 +37,6 @@ public: } bool post(long count = 1) { - if (count <= 0) return true; return !!::ReleaseSemaphore(h_, count, NULL); } }; @@ -116,10 +115,12 @@ public: void broadcast(handle_t& h) { if (h == invalid()) return; IPC_UNUSED_ auto guard = ipc::detail::unique_lock(mtx(h)); - sem(h).post(counter_); - while (counter_ > 0) { - -- counter_; - han(h).wait(); + if (counter_ > 0) { + sem(h).post(counter_); + do { + -- counter_; + han(h).wait(); + } while (counter_ > 0); } } };