signal handling

Discussion in 'C++' started by Lionel van den Berg, Mar 22, 2005.

  1. Hi all,

    I'm trying to do some signal handling using the csignal library but I
    can't find and specific examples for C++.

    The signal function as defined in C takes a parameter that is the signal
    we are registering for, and a second parameter that is the function to call.

    What I want to do is specify the function to call as a function of an
    instance of "this" class e.g.

    void Class::signalHandler( int signal ){}

    void Class::someFunction( ) {
    signal( SIGINT, this.signalHandler );
    }


    Obviously this is wrong because it complains about signalHandler not
    being a type.

    How do I achieve this in C++? Can I achieve this in C++?

    Thanks,

    Lionel.
    Lionel van den Berg, Mar 22, 2005
    #1
    1. Advertising

  2. * Lionel van den Berg:
    >
    > I'm trying to do some signal handling using the csignal library but I
    > can't find and specific examples for C++.
    >
    > The signal function as defined in C takes a parameter that is the signal
    > we are registering for, and a second parameter that is the function to call.
    >
    > What I want to do is specify the function to call as a function of an
    > instance of "this" class e.g.
    >
    > void Class::signalHandler( int signal ){}
    >
    > void Class::someFunction( ) {
    > signal( SIGINT, this.signalHandler );
    > }
    >
    >
    > Obviously this is wrong because it complains about signalHandler not
    > being a type.
    >
    > How do I achieve this in C++? Can I achieve this in C++?


    You don't. 'signal' is generally incompatible with C++. AFAIK the only
    things you can reliably do in a handler are (1) to terminate, (2) to do
    nothing, or (3) set a global flag of type 'volatile sig_atomic_t', and the
    standard notes in particular that a signal handler using exception handling
    is likely to have problems.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 22, 2005
    #2
    1. Advertising

  3. Lionel van den Berg

    Stephen Howe Guest

    > Obviously this is wrong because it complains about signalHandler not being
    > a type.
    >
    > How do I achieve this in C++?


    You can't register member functions as a signal handler. Obviously so,
    because "this" points to a specific instance of your class with its
    associated class data. When your signal is raised, how does it know what
    "this" to supply?

    class member functions and function pointers are incompatibile.

    What you can register is a static class function, because a static class
    function is not tied to any specific instance of a class.
    static class functions and function pointers can be compatible.

    Stephen Howe
    Stephen Howe, Mar 23, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Pronath
    Replies:
    1
    Views:
    1,166
    Diez B. Roggisch
    Jan 3, 2005
  2. Jack Orenstein

    threading.Thread vs. signal.signal

    Jack Orenstein, Sep 18, 2005, in forum: Python
    Replies:
    0
    Views:
    463
    Jack Orenstein
    Sep 18, 2005
  3. Weng Tianxiang
    Replies:
    2
    Views:
    658
    Jonathan Bromley
    Jan 30, 2007
  4. Nicolas Moreau
    Replies:
    9
    Views:
    3,144
  5. Peter
    Replies:
    34
    Views:
    1,936
    James Kanze
    Oct 17, 2009
Loading...

Share This Page