Receive messages till atomic remaining count is 0. remaining is shared among all receiving threads
| 245 | // Receive messages till atomic remaining count is 0. |
| 246 | // remaining is shared among all receiving threads |
| 247 | void receive_thread(receiver& r, std::atomic_int& remaining) { |
| 248 | try { |
| 249 | auto id = std::this_thread::get_id(); |
| 250 | int n = 0; |
| 251 | // atomically check and decrement remaining *before* receiving. |
| 252 | // If it is 0 or less then return, as there are no more |
| 253 | // messages to receive so calling r.receive() would block forever. |
| 254 | while (remaining-- > 0) { |
| 255 | auto m = r.receive(); |
| 256 | ++n; |
| 257 | OUT(std::cout << id << " received \"" << m.body() << '"' << std::endl); |
| 258 | } |
| 259 | OUT(std::cout << id << " received " << n << " messages" << std::endl); |
| 260 | } catch (const closed&) {} |
| 261 | } |
| 262 | |
| 263 | int main(int argc, const char **argv) { |
| 264 | try { |