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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:59:51+00:00 2026-06-07T07:59:51+00:00

I am getting acquainted with boost thread and signals. I am thus implementing this

  • 0

I am getting acquainted with boost thread and signals. I am thus implementing this simple example, I only post the cpp file of an example class implementing a thread capable of executing a method when a Signal1 is fired. The signal is defined within a Package1Signals singleton (excuse me for these names, they have been generated from a model)

Class1.hpp

#ifndef CLASS1_HEADER
#define CLASS1_HEADER
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/udp.hpp>
#include <boost/asio/signal_set.hpp>
#include "Package1.hpp"
#include <boost/thread.hpp>


class Class1{
private:
    boost::asio::io_service service;
public:
    boost::thread  thread;
    Class1();
    void classifierBehavior();
    void Reception1(Signal1 signal1);
    void Reception1Behavior();
};


#endif

Class1.cpp

#include "Class1.hpp"
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
#include "Package1.hpp"
#include "Package2.hpp"
#include "Package1.hpp"

Class1::Class1(){
 //boost::thread thread(boost::bind(&Class1::classifierBehavior,boost::ref(*this)));
 //thread.join();
 thread = boost::thread(&Class1::classifierBehavior,this);
};

void Class1::classifierBehavior(){
  Package1Signals::getInstance()->signal1.connect(boost::bind(&Class1::Reception1, 
  this,_1));
  service.run();

};

 void Class1::Reception1(Signal1 signal1){
std::cout<<"Signal received\n";
service.post(boost::bind(&Class1::Reception1Behavior, this));
 }

 void Class1::Reception1Behavior(){
std::cout<<"Behavior executed\n";
 }

Package1.hpp

#ifndef PACKAGE1_HEADER
#define PACKAGE1_HEADER
#include <boost/signal.hpp>

struct Signal1{   };

class Package1Signals{
private:
    Package1Signals();
    static Package1Signals * instance;
public:
    boost::signal<void(Signal1)> signal1;
    static Package1Signals * getInstance();
 };
 #endif

Package1.cpp

 #include "Package1.hpp"
 Package1Signals * Package1Signals::instance = NULL;
 Package1Signals::Package1Signals(){}
 Package1Signals * Package1Signals::getInstance(){
    if(!instance){
            instance = new Package1Signals();
        }
    return instance;
 }

here is the code which executes it

int main() {
    Class1 test;
    Package1Signals::getInstance()->signal1();
    test.thread.join();
    int k =0;
    std::cin>>k;
    return 0;
 }

I can see the thread runs, the signal is intercepted but the posted handler is not executed. What am I doing wrong?

  • 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-07T07:59:52+00:00Added an answer on June 7, 2026 at 7:59 am

    I can see the thread runs, the signal is intercepted

    Are you sure? I don’t think the signal is event sent.

    but the posted handler is not executed. What am I doing wrong?

    Your constructor creates a new thread, then waits for it to finish, so the constructor doesn’t return until the new thread exits. That means this line in main won’t execute until the receiver thread exits:

     Package1Signals::getInstance()->signal1();
    

    Maybe you want the boost::thread to be a member of your class, so it has the same lifetime as your class, instead of being a local variable in the constructor.

    For style considerations, you don’t need to use boost::ref(*this) you can just pass this, and you don’t need to use boost:bind to create the thread, read the docs which tell you that constructing a thread with multiple arguments is equivalent to passing those arguments to bind and constructing the thread with the result i.e. you can just say

    thread(&Class1::classifierBehavior, this);
    

    Which is much simpler and easier to read!

    Update: Now you’ve fixed the constructor so it doesn’t block you have a race condition between connecting the receiver to the signal, which happens in the new thread, and publishing the signal, which happens in the main thread, as soon as the constructor finishes. If the new thread takes a few milliseconds to start executing then it will be too late and miss the signal, because in the main thread the constructor will have finished and the signal will have been emitted already.

    Try connecting the receiver before starting the new thread:

    Class1::Class1(){
      Package1Signals::getInstance()->signal1.connect(boost::bind(&Class1::Reception1,   this,_1));
     thread = boost::thread(&Class1::classifierBehavior,this);
    };
    
    void Class1::classifierBehavior(){
      service.run();
    };
    

    This way the receiver is connected before the signal is emitted.

    I think the io_service will still get the event even if the event gets posted before the service is running, but you might want to check that, otherwise there’s another race condition there.

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

Sidebar

Related Questions

Getting this error when I follow the simple code example from jsoncpp, that basically
I'm just getting acquainted with implementing REST web services in Java using JAX-RS and
Getting very annoyed with this simple query... I need to add an offset to
GETTING THIS ERROR WHEN UPLOADING A FILE : LoadError (no such file to load
I'm just getting acquainted with Vim's CTags functionality - and it's damn handy. One
I'm getting myself acquainted with MVC by making a 'for-fun' site in django. I'm
I'm getting acquanted with Core Animation and trying to implement simple movement along a
Getting ERR_CONNECTION_RESET after more or less 2 minutes of uploading a rather big file
Getting 'Dispatcher has no subscribers' error while trying to post message to a channel
Getting red warning message that expected identifier for this statement CALayer = ImageView.layer; 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.