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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:22:33+00:00 2026-06-12T16:22:33+00:00

I have an object which does some work in an endless loop. The main()

  • 0

I have an object which does some work in an endless loop. The main() instantiates the object and calls the run() method. Since I don’t want to use threads, I need a solution to make my object stop running. Below you see what I’ve come up with.

struct Foo
{
    void run()
    {
        running = 1;
        while (running) 
            do_something_useful();

        std::cout << "Execution stopped." << std::endl;
    }

    bool running;

    void catch_signal(int signal)
    {
        std::cout << "Caught signal " << signal << std::endl;
        if( signal == SIGTERM ) 
            running = false;
    }

};

As you see, I need to send a signal asynchronously. Therefore, I use a signal handler and sigaction. Below the main I can imagine to use.

int main(int argc, char** argv)
{
    Foo foo;
    struct sigaction sigIntHandler;

    boost::function< void (int) > f;
    f = std::bind1st(
      std::mem_fun(&Foo::catch_signal), &foo);
    f(5);  // this call works

    sigIntHandler.sa_handler = f;           // compiler complains, "cannot assign ..."
    sigemptyset(&sigIntHandler.sa_mask);
    sigIntHandler.sa_flags = 0;
    sigaction(SIGTERM, &sigIntHandler, NULL);
    s.run();

}

What I would expect now: The program runs until I send SIGTERM which is caught and will cause my object to stop iteration and return to main.

I have two questions now:

(a) In the code you see a line marked with “Compiler complains”, the message is like

boost::function<void(int)> cannot be converted to __sighandler_t {aka void (*)(int)}

What do I need to change to make this work? I think f is like void f(int), like the functions the signal handler gets in some examples.

(b) For those of you who wonder “what is that guy doing?”: Do you have any advice how to solve this kind of thing better?

  • 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-12T16:22:34+00:00Added an answer on June 12, 2026 at 4:22 pm
    • What do I need to change to make this work? I think f is like void f(int), like the functions the signal handler gets in some examples.

    The compiler complains about the type, therefore you need to pass a function pointer, not an object of type boost::function<void(int)>. Creating a global variable of this type, and adding a function which calls this object would work :

    boost::function<void(int)> myCb;
    void CallCb( int value )
    {
      myCb(value);
    }
    
    int main(int argc, char** argv)
    {
        Foo foo;
        struct sigaction sigIntHandler;
    
        myCb = std::bind1st(
          std::mem_fun(&Foo::catch_signal), &foo);
        f(5);  // this call works
    
        sigIntHandler.sa_handler = CallCb;
        sigemptyset(&sigIntHandler.sa_mask);
        sigIntHandler.sa_flags = 0;
        sigaction(SIGTERM, &sigIntHandler, NULL);
        s.run();
    
    }
    
    • Do you have any advice how to solve this kind of thing better?

    Not really. The idea is ok. I would just just c++11 lambda instead

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

Sidebar

Related Questions

I have a helper function, which basically calls CompareTo on two objects, but does
I have a UserControl with something that does some background work. When thing has
I have constructed a generic method which deserialiazes an object in the following manner:
I have written a multithreaded program which does some thinking and prints out some
What I want to have is a custom object which provides some events. For
I have a Day object which contains some statistics and is updated daily. Here's
I have object A which contains multiple instances of object B, which in turn
I have this object which is an instance of a superclass. I want to
I have an object which contains models for my ASP.NET MVC web app. The
I have an object which has several properties that are set when the object

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.