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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:20:18+00:00 2026-05-28T14:20:18+00:00

I’m writing a multi-threaded server using boost::asio (for sockets), boost::thread (for threading), libconfig++ (for

  • 0

I’m writing a multi-threaded server using boost::asio (for sockets), boost::thread (for threading), libconfig++ (for configuration files reading) and protocol buffers (for the protocol implementation).

The server follows more or less this route: main() -> creates an Application object -> runs application object. Application loads configuration file, then creates the server object (which is passed the configuration class as a const). Server object configures itself and binds the port, starts accepting, blah. Whenever a new client is detected, the server creates a new Client object, and then creates a thread running the client’s connection handler.

All of this is to explain that the configuration file is loaded from my Application class, and then passed all the way down to my Client class. This shouldn’t pose any kind of trouble if the libconfig object was passed directly all the way to the Client, yet as we all know, multi-threading implies that memory corrupts when accessed simultaneously by two or more threads.

The way to solve this was discussed in other post and ended up with the implementation of a wrapper which automagically solves the mutex problem.

The magical class

app_config.h

#ifndef _APP_CONFIG_H_
#define _APP_CONFIG_H_ 1

#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp>
#include <boost/noncopyable.hpp>
#include <libconfig.h++>
#include <string>

namespace BBCP {
    namespace App {

class ConfigLock;

class Config {
public:
    friend class BBCP::App::ConfigLock;

    Config(std::string const &file) :
        cfg(new libconfig::Config()),
        mutex(new boost::mutex())
    {
        cfg->readFile(file.c_str());
    }

private:
    boost::shared_ptr<libconfig::Config> cfg;
    boost::shared_ptr<boost::mutex> mutex;
};

class Server;
class Client;

class ConfigLock : boost::noncopyable {
public:
    ConfigLock(BBCP::App::Config const &wrapper) :
        cfg(wrapper.cfg),
        mutex(wrapper.mutex),
        lock(new LockType(*mutex))
    { }

    libconfig::Config &get() throw() { return *cfg; };
private:
    boost::shared_ptr<libconfig::Config> cfg;
    boost::shared_ptr<boost::mutex> mutex;

    typedef boost::lock_guard<boost::mutex> LockType;
    boost::shared_ptr<LockType> lock;
};

    }
}

#endif

For lazy people, this class consists of… well, two classes (irony?): BBCP::App::Config and BBCP::App::ConfigLock. BBCP::App::Config simply loads a file, while BBCP::App::ConfigLock takes a BBCP::App::Config as an argument, and then locks BBCP::App::Config‘s mutex. Once the lock has been created, one calls BBCP::App::ConfigLock::get, which returns a reference to the libconfig::Config object!.

THE problem

Well:

server.cpp:

void BBCP::App::Server::startAccept() {
    newClient.reset(new BBCP::App::Client(io_service, config_wrapper));
    acceptor.async_accept(newClient->getSocket(), boost::bind(&BBCP::App::Server::acceptHandler, this, boost::asio::placeholders::error));
}

This function creates a new client object, loaded with the boost::asio::io_service object and BBCP::App::Config object.

server.cpp

void BBCP::App::Server::acceptHandler(boost::system::error_code const &e) {
    if (!acceptor.is_open()) {
        // ARR ERROR!
        return;
    }

    if (!e) {
        client_pool.create_thread(*newClient);
    }
    else {
        // HANDLE ME ERROR
        throw;
    }

    startAccept();
}

This function creates a new thread or (not implemented yet) errors in case of… well, errors, then starts the accept loop again.

Client code mostly doesn’t matter until this part:

client.cpp:

void BBCP::App::Client::parseBody() {
    BBCP::Protocol::Header header;
    BBCP::Protocol::Hello hello;
    boost::scoped_ptr<BBCP::App::ConfigLock> lock;
    libconfig::Config *cfg;

    (...)

    switch ((enum BBCP::Protocol::PacketType)header.type()) {
        case BBCP::Protocol::HELLO:
            (...)

            // config_wrapper is a private variable in the client class!
            lock.reset(new BBCP::App::ConfigLock(config_wrapper));

            // ARRRRRRR HERE BE DRAGOONS!!
            *cfg = lock->get();

            (...)

            lock.reset();
            break;
        (...)

    }

    (...)
}

Well, truth be told, I didn’t expect this kind of error:

/usr/include/libconfig.h++: In member function ‘void BBCP::App::Client::parseBody()’:
/usr/include/libconfig.h++:338:13: error: ‘libconfig::Config& libconfig::Config::operator=(const libconfig::Config&)’ is private
client.cpp:64:30: error: within this context
client.cpp:71:21: error: request for member ‘exists’ in ‘cfg’, which is of non-class type ‘libconfig::Config*’
client.cpp:77:51: error: request for member ‘lookup’ in ‘cfg’, which is of non-class type ‘libconfig::Config*’

But here it is, and I need some way to solve it :(. I’ve tried making BBCP::App::Client a friend class of BBCP::App::ConfigLock, but then it went like:

In file included from ../include/app_config.h:4:0,
                 from ../include/app_main.h:6,
                 from main.cpp:18:
../include/app_client.h:15:53: error: ‘BBCP::App::Config’ has not been declared
In file included from ../include/app_config.h:4:0,
                 from ../include/app_main.h:6,
                 from main.cpp:18:
../include/app_client.h:32:5: error: ‘Config’ in namespace ‘BBCP::App’ does not name a type
In file included from ../include/app_config.h:4:0,
                 from ../include/app_main.h:6,
                 from main.cpp:18:
../include/app_client.h: In constructor ‘BBCP::App::Client::Client(boost::asio::io_service&, const int&)’:
../include/app_client.h:15:120: error: class ‘BBCP::App::Client’ does not have any field named ‘config_wrapper’

And then I went like O_O, so I just gave up and came here, once again looking for some über C++ guru hackz0r’s help and scolding for doing such a misdeed as trying to access another class’s private members is.

  • 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-28T14:20:19+00:00Added an answer on May 28, 2026 at 2:20 pm

    You probably wanted to store a pointer to the config object in cfg instead of creating a copy (and dereferencing an uninitialized pointer):

    cfg = &local->get();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload

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.