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

  • SEARCH
  • Home
  • 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 7190469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:31:21+00:00 2026-05-28T19:31:21+00:00

To learn c++11 (and boost) I am writing a simple http-server using boost asio

  • 0

To learn c++11 (and boost) I am writing a simple http-server using boost asio and c++11 (for threading and lambdas).

I want to test the new c++11 lambdas and std::thread so I tried to start the io_service.run() like this in a std::thread with a lambda:

#include <iostream>
#include <thread>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
using std::cout;
using std::endl;
using boost::asio::ip::tcp;

class HttpServer
{
public:
    HttpServer(std::size_t thread_pool_size)
    : io_service_(),
    endpoint_(boost::asio::ip::tcp::v4(), 8000),
    acceptor_(io_service_, endpoint_)
    { }

    void Start() {
        acceptor_.listen();
        cout << "Adr before " << &io_service_ << endl;
        std::thread io_thread([this](){
            cout << "Adr inside " << &io_service_ << endl;
            io_service_.run();
        });
        io_thread.join();
    }

private:
    boost::asio::io_service io_service_;
    tcp::endpoint endpoint_;
    tcp::acceptor acceptor_;
};

int main() {
    HttpServer server(2);
    server.Start();
}

This terminates with segmentation fault. Additionally sometimes it is running the cout inside the lambda, sometimes not (although endl should flush). In any case, it prints the correct address of io_service_. However, when I replace the std::thread with a boost::thread (no other change!), everything is working fine.

I would appreciate it if anyone has an idea where the problem is caused (could be asio, std::thread or std::lambda).

Additional information:

According to another post accessing the member io_service_ within a lambda is fine when capturing this, as I do it.

I am running gcc 4.6.1 on Ubuntu and boost 1.46. G++ parameters:

g++ -std=c++0x -static -I/home/andre/DEV/boost_1_48_0/include/ -L/home/andre/DEV/boost_1_48_0/lib/ -o webserver main.cpp -lboost_system -lboost_thread -lpthread

Update:

Removing -static fixed the problem. I found out that the problem has nothing to do with boost or lambdas and can be reproduced whenever building static and using std::thread. For any reason this combination does not work.
I think this post describes almost the same, however I don’t really understand the details and the error message is different.

So I wonder why std::thread and static linking don’t seem to work together. Is there a reason why static linking is not allowed here? I updated the question title and removed the boost tag.

  • 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-28T19:31:22+00:00Added an answer on May 28, 2026 at 7:31 pm

    Removing -static solved the problem. I found out that it has nothing to do with boost or lambdas, but with static linking and std::thread, which don’t seem to work together for any unknown reason (see also this post that might be related).

    I guess the question why they don’t work together is – though interesting – out of scope for now, and I am glad with the answer, so this can be marked as answered.

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

Sidebar

Related Questions

I was trying to learn how to re assign accepted connection using Boost.ASIO and
I'm trying to learn boost::asio for socket/networking programming. I'm trying to send some simple
I'm trying to learn a little bit C++ and Boost.Asio. I'm trying to compile
I'm trying to learn boost::statechart. I want to make a little app which loads
I am trying to learn how to use the C++ Boost.Thread library. I have
I am curious to learn Boost . But I wanted to ask: How important
I try to learn how Boost.Spirit works. Unfortunately, the documentation is too laconic for
To learn GWT and Google AppEngine (GAE) I'm trying to create a simple application
Currently I`m using Visual Studio for writing code in C++. But it seems so
I'm trying to learn Boost Spirit and as an exercise, I've tried to parse

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.