channel::send will wait for a while & return false if there is no receiver

This commit is contained in:
mutouyun 2018-12-24 12:11:50 +08:00
parent 0eb61b8745
commit b815a5e50f
2 changed files with 4 additions and 3 deletions

View File

@ -134,7 +134,8 @@ std::size_t channel::recv_count() const {
template <typename... P>
inline bool channel_send(route& rt, P&&... params) {
while (rt.recv_count() == 0) {
for (int k = 0; rt.recv_count() == 0; ++k) {
if (k >= 1024) return false;
std::this_thread::yield();
}
return rt.send(params...); // no need std::forward

View File

@ -425,7 +425,7 @@ void Unit::test_channel_rtt() {
auto dd = cc.recv();
if (dd.size() < 2) return;
// std::cout << "recving: " << i << "-[" << dd.size() << "]" << std::endl;
cc.send(ipc::buff_t { 'a' });
while (!cc.send(ipc::buff_t { 'a' })) {}
}
}};
@ -434,7 +434,7 @@ void Unit::test_channel_rtt() {
sw.start();
for (std::size_t i = 0; i < LoopCount; ++i) {
// std::cout << "sending: " << i << "-[" << datas__[i].size() << "]" << std::endl;
cc.send(datas__[i]);
while (!cc.send(datas__[i])) {}
/*auto dd = */cc.recv();
// if (dd.size() != 1 || dd[0] != 'a') {
// QVERIFY(false);