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 8329863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:56:11+00:00 2026-06-09T01:56:11+00:00

I have the following code using Boost ASIO to setup a TCP client. Here

  • 0

I have the following code using Boost ASIO to setup a TCP client. Here is my code adapted from the Boost doc’s chat example.

class AsioCommunicationService {
AsioCommunicationService::AsioCommunicationService(
        boost::asio::io_service& io_service,
        tcp::resolver::iterator endpoint_iterator)
    : io_service_(io_service),
      socket_(io_service)
{
    tcp::endpoint endpoint = *endpoint_iterator;
    socket_.async_connect(endpoint,
    boost::bind(&AsioCommunicationService::handle_connect, this,
    boost::asio::placeholders::error, ++endpoint_iterator));
}

void AsioCommunicationService::handle_connect(const boost::system::error_code& error,
        tcp::resolver::iterator endpoint_iterator)
{
    if (!error)
    {
      boost::asio::async_read(socket_,
          boost::asio::buffer(read_msg_.data(), LampMessage::header_length),
          boost::bind(&AsioCommunicationService::handle_read_header, this,
          boost::asio::placeholders::error));
    }
}
}


class Connection
{
    //init io_service, query, resolve, iterator here
    boost::asio::io_service io_service;
    boost::asio::ip::tcp::resolver resolver(io_service);
    boost::asio::ip::tcp::resolver::query query(host, service);
    boost::asio::ip::tcp::resolver::iterator endpoint_iterator =
  resolver.resolve(query);


    m_session = std::shared_ptr<AsioCommunicationService>(
            new AsioCommunicationService(io_service, iterator));

    //start new thread for io_service.run --> GOT AN ERROR when include boost/thread.hpp
    boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));

    //this synchronous command would work, but it's blocking the program. I don't want that.
    //io_service.run();
}

Of course, I needed to include boost/thread to make the declaration to variable t in class Connection works. But when I did so, I got this error

#include <boost/thread.hpp>
//ERROR: In function ‘boost::thread&& boost::move(boost::thread&&)’:
///usr/include/boost/thread/detail/thread.hpp:349:16: error: invalid initialization of reference of type ‘boost::thread&&’ from expression of type ‘boost::thread’
//In file included from /usr/include/boost/thread/detail/thread_heap_alloc.hpp:17:0,
//             from /usr/include/boost/thread/detail/thread.hpp:13,
//             from /usr/include/boost/thread/thread.hpp:22,
//             from /usr/include/boost/thread.hpp:13,
//             from /home/son/dev/logistics/src/frameworks/networkService/NetworkConnection.cpp:13:
///usr/include/boost/thread/pthread/thread_heap_alloc.hpp: In function ‘T* boost::detail::heap_new(A1&&) [with T = boost::detail::thread_data<void (*)()>, A1 = void (*&)()]’:
///usr/include/boost/thread/detail/thread.hpp:130:95:   instantiated from here
///usr/include/boost/thread/pthread/thread_heap_alloc.hpp:24:47: error: cannot bind ‘void (*)()’ lvalue to ‘void (*&&)()’
///usr/include/boost/thread/detail/thread.hpp:43:13: error:   initializing argument 1 of ‘boost::detail::thread_data<F>::thread_data(F&&) [with F = void (*)()]’

It would compile and work if I remove the include to boost/thread.hpp, and replace the declaration to t by a simple call to io_service.run(); I’m wondering if this compilation error has to do with boost version. I’m using Boost ASIO 1.42, Ubuntu 11.04 and Eclipse if those are of any help. Thank you in advance.

  • 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-06-09T01:56:13+00:00Added an answer on June 9, 2026 at 1:56 am

    After much exploration, following @MvG suggestion, I successfully compiled and linked my simple program by doing the following:

    • manually build and install boost 1.50 (using MvG suggestion)
    • compile and link the source using the following 2 make files (1 for static and 1 for dynamic linking).

    Dynamic:

    default: test
    
    test.o: test.cpp
        g++-4.5 -std=c++0x -I /home/son/boost_1_50/include/ -c test.cpp 
    
    test: test.o        
        g++-4.5 -std=c++0x -L /home/son/boost_1_50/lib/ test.o -lboost_thread -lboost_system -lboost_chrono -pthread -o test
    
    run: test
        LD_LIBRARY_PATH=/home/son/boost_1_50/lib/ ./test
    

    Static:

    default: test
    
    test.o: test.cpp
        g++-4.5 -std=c++0x -I /home/son/boost_1_50/include/ -c test.cpp 
    
    test: test.o
        g++-4.5 -std=c++0x -L /home/son/boost_1_50/lib/ test.o -static -lboost_thread -lboost_system -lboost_chrono -pthread -o test
    
    run: test
        ./test
    

    The test.cpp file is as follows.

    #include <boost/thread.hpp>
    #include <stdio.h>
    
    int main()
    {
        printf("boost thread tested by son\n");
        boost::thread t;
        return 0;
    }
    

    That’s it. I’m having difficulties compiling this with CMake but that’s another problem.

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

Sidebar

Related Questions

I have the following code: #include <iostream> #include boost/shared_ptr.hpp using boost::shared_ptr; class Base {
I have the following code: #include <iostream> #include boost/unordered_map.hpp using namespace std; using namespace
I have the following code: #include <boost/smart_ptr/shared_ptr.hpp> #include <boost/numeric/interval.hpp> #include <boost/math/distributions/students_t.hpp> using namespace boost::numeric;
I have the following code that saves a map into shared memory using boost
I have written the following code #include <iostream> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/filesystem.hpp>
I have following code using hibernate to throw a custom exception on error and
I have the following code using actionscript and the indesign sdk: At the beginning
I have the following code: using System; using System.Diagnostics; using System.IO; using PdfSharp.Pdf.Printing; namespace
I have the following code: using (BinaryReader br = new BinaryReader( File.Open(FILE_PATH, FileMode.Open, FileAccess.ReadWrite)))
I have the following code using jQuery Validate. $(#register).validate({ debug: true, errorClass:'error', validClass:'success', errorElement:'span',

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.