MCPcopy Create free account
hub / github.com/apache/qpid-proton / main

Function main

cpp/examples/multithreaded_client_flow_control.cpp:263–316  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

261}
262
263int main(int argc, const char **argv) {
264 try {
265 if (argc != 5) {
266 std::cerr <<
267 "Usage: " << argv[0] << " CONNECTION-URL AMQP-ADDRESS MESSAGE-COUNT THREAD-COUNT\n"
268 "CONNECTION-URL: connection address, e.g.'amqp://127.0.0.1'\n"
269 "AMQP-ADDRESS: AMQP node address, e.g. 'examples'\n"
270 "MESSAGE-COUNT: number of messages to send\n"
271 "THREAD-COUNT: number of sender/receiver thread pairs\n";
272 return 1;
273 }
274
275 const char *url = argv[1];
276 const char *address = argv[2];
277 int n_messages = atoi(argv[3]);
278 int n_threads = atoi(argv[4]);
279 int count = n_messages * n_threads;
280
281 // Total messages to be received, multiple receiver threads will decrement this.
282 std::atomic_int remaining;
283 remaining.store(count);
284
285 // Run the proton container
286 proton::container container;
287 auto container_thread = std::thread([&]() { container.run(); });
288
289 // A single sender and receiver to be shared by all the threads
290 sender send(container, url, address);
291 receiver recv(container, url, address);
292
293 // Start receiver threads, then sender threads.
294 // Starting receivers first gives all receivers a chance to compete for messages.
295 std::vector<std::thread> threads;
296 threads.reserve(n_threads*2); // Avoid re-allocation once threads are started
297 for (int i = 0; i < n_threads; ++i)
298 threads.push_back(std::thread([&]() { receive_thread(recv, remaining); }));
299 for (int i = 0; i < n_threads; ++i)
300 threads.push_back(std::thread([&]() { send_thread(send, n_messages); }));
301
302 // Wait for threads to finish
303 for (auto& t : threads) t.join();
304 send.close();
305 recv.close();
306 container_thread.join();
307 if (remaining > 0)
308 throw std::runtime_error("not all messages were received");
309 std::cout << count << " messages sent and received" << std::endl;
310
311 return 0;
312 } catch (const std::exception& e) {
313 std::cerr << e.what() << std::endl;
314 }
315 return 1;
316}

Callers

nothing calls this directly

Calls 6

receive_threadFunction · 0.85
send_threadFunction · 0.85
joinMethod · 0.80
runMethod · 0.45
closeMethod · 0.45
whatMethod · 0.45

Tested by

no test coverage detected