Quantcast
Channel: How to concurrently send data to multiple servers using C++ socket programming? - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Answer by Mouze for How to concurrently send data to multiple servers using C++ socket programming?

$
0
0

Not sure this is the only problem, but given what you shared, the main thread is not waiting for the worker threads to finish their tasks.

Per this answer: When the main thread exits, do other threads also exit?, the process will be terminated when the main thread returns from main().

Proposed fix:

int main() {    // .. your logic here    std::vector<std::thread> threads;    for (int i = 0; i < 4; ++i) {        std::thread t(send_data, server_ips[i], port, bufdata);         threads.push_back(std::move(t));    }    for (int i = 0; i < 4; ++i) {        if (threads[i].joinable())            threads[i].join(); // wait for threads[i] to finish    }    // .. clean up}

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>