Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6570845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:48:27+00:00 2026-05-25T14:48:27+00:00

I created a console application that sends data on a network link. I used

  • 0

I created a console application that sends data on a network link. I used the boost library, both the thread and the asio ones; currently i’m running it under Windows. If I run a single application it works perfectly, but if I open two instances on two different consoles, the CPU load goes to 100%, if I close one of the application it goes back to normal. I just used a simple socket with async reads and writes, and threads with condition variables and mutexes. Is there any special thingh to do when dealing with such a situation? I can show you some code, but I think it’s nothing special:

socket->connect(tcp::endpoint(address::from_string(getAddress()),getPort()));

for connecting

and

socket->async_read_some(buffer(receiveData),bind(&NetworkLink::handle_response, this,placeholders::error,placeholders::bytes_transferred));

inside the handle_response function for async reading.
For the thread I use

boost::unique_lock<boost::mutex> messages_lock(message_received_mutex);

Before deleting everything and starting a simple test project from scratch I would like to know if there are any special care to be taken in this situation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:48:27+00:00Added an answer on May 25, 2026 at 2:48 pm

    Ok it seems I’ve tracked down the problem. First of all, the 100% CPU usage was due to the fact that each instance was using 50% of the CPU (I’m on a dual core PC). So I run all over the code and found out this. I had this in my code, inside the NetworkLink::handle_response function:

    socket->async_read_some(
        boost::asio::buffer(receiveData),
        boost::bind(&NetworkLink::handle_response, this,
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));
    io_service.run();
    

    I used the io_service.run(); because before the software was not receiving data. Now I removed the line, I don’t get the 50% CPU usage, but the handler response is not called so I cannot receive any data. Any thought about this?
    Thanks

    PS: I created a small app that shows this problem:

    #include <iostream>
    #include <boost/asio.hpp>
    #include "boost/thread.hpp"
    #include "boost/thread/mutex.hpp"
    
    #ifdef _WIN32
    #include "Windows.h"
    #endif
    
    using namespace boost::asio::ip;
    using namespace std;
    
    std::vector<uint8_t> buf;
    boost::asio::io_service io_service;
    boost::asio::ip::tcp::socket mysocket(io_service);
    
    int handle_response(const boost::system::error_code &err,
            size_t bytes_transferred)
    {
        // cout << bytes_transferred << ' ';
        if (bytes_transferred > 0)
            cout << buf.data() << ' ';
        boost::asio::async_read(mysocket, boost::asio::buffer(buf),
                boost::asio::transfer_at_least(1), &handle_response);
    }
    
    int mythread()
    {
        boost::asio::async_read(mysocket, boost::asio::buffer(buf),
                boost::asio::transfer_at_least(1), &handle_response);
    
        io_service.run();
    }
    
    int main()
    {
        int m_nPort = 12345;
    
        buf.resize(100, '0');
        boost::condition_variable message_received_condition;
        boost::thread message_receiver_thread(&mythread);
        boost::mutex messages_mutex;
    
        tcp::endpoint endpoint(boost::asio::ip::address::from_string("127.0.0.1"),
                m_nPort);
    
        boost::unique_lock<boost::mutex> messages_lock(messages_mutex);
        message_received_condition.notify_one();
    
        cout << "Waiting for connection..." << endl;
    
        Sleep(10000);
        mysocket.connect(endpoint);
        cout << "connection accepted" << endl;
        try
        {
            while (true)
            {
                boost::system::error_code ec;
                boost::asio::socket_base::bytes_readable command(true);
                mysocket.io_control(command);
                std::size_t bytes_readable = command.get();
    
                mysocket.async_read_some(boost::asio::buffer(buf),
                        &handle_response);
    
                io_service.run();
            }
        } catch (exception &e)
        {
            cerr << e.what() << endl; //"The parameter is incorrect" exception
        }
    }
    

    If you remove the comment from th line
    // cout << bytes_transferred << ‘ ‘;
    in the handle response functionyou get a lower CPU usage, I guess because of the delay for writing to the screen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a console application that gathers some data and then sends it
I have created a console application that calls a method on a webservice. I
I'm exploring MSMQ services, and I wrote a simple console client-server application that sends
I am using Delphi 2010, and when I created a console application that prints
I have a C# console application that fetches data from more than 100 servers
I have a console application that sends customized emails (with attachments) to different recipients
I'm writing a console application that does a good amount of data retrieval from
I have created a small daemon (basically a console application that hides the console
I have a long-running console-based application Sender that sends simple text to STDOUT using
I have a long-running console-based application Sender that sends simple text to STDOUT using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.