POSIX timers in c++

This is a discussion on POSIX timers in c++ within the C++ Programming forums, part of the General Programming Boards category; hello all, I'm using POSIX timers in C++ application, using timer_create() this timer will raise a signal when timer expires ...

  1. #1
    suo
    suo is offline
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    POSIX timers in c++

    hello all,

    I'm using POSIX timers in C++ application, using timer_create() this timer will raise a signal when timer expires and to Handle this I want to use signal handler function (which is in my class method).

    To achieve this I need to make this class method as static method, now if I wanna use any attribute in class that should be static variable!!! Is there any alternative method to achieve this ????


    Thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > I want to use signal handler function (which is in my class method).
    Which instance (of an object) do you want the signal to apply to?

    This only seems to make sense if you have a singleton.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You can store a pointer to your class in the sigevent structure, then access that pointer in the signal handler.
    Code:
    struct sigevent e;
    e.sigev_value.sival_ptr = myClassPointer;
    // ...
    Then in your signal handler:
    Code:
    void MyClass::StaticHandler(int sig, siginfo_t *si, void *uc)
    {
        MyClass* c = reinterpret_cast<MyClass*>(si−>si_value.sival_ptr);
        c->foo();
    }

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    3
    thanks to bithub for your answer.

    I tried to solve the problem this way but I got some problems to set up the right signal handler function.

    If I tried to set the extended handler function - all I got was this error:

    Code:
    void Scheduler::tic(int sigNumb, siginfo_t *si, void *uc) {
    	perror("i was called");
    }
    
    this->SignalAction.sa_handler = Scheduler::tic;
    
    error: invalid conversion from 'void (*)(int, siginfo_t*, void*)' to 'void (*)(int)'
    Am I right in setting the handler to sa_handler ?

    By the way... am I allowed to pass references to class members for setting up the timer, as follows:

    Code:
    timer_create(CLOCK_REALTIME, &this->signalEvent, &this->timerID)
    thanks for your help

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    3
    To use a signalhandler function with the signature

    Code:
    void func(int signo, siginfo_t *info, void *context)
    The flag SA_SIGINFO has to be set in sigaction.sa_flags and the callback-function pointer has to be added to sigaction.sa_sigaction.

    Code:
           struct sigevent signalEvent;
    	
    	struct sigaction SignalAction;
    
           // Define sigaction method
    	this->SignalAction.sa_sigaction = Scheduler::tic;
    	// set mask empty -> react on all signals
    	sigemptyset(&this->SignalAction.sa_mask);
    	this->SignalAction.sa_flags = SA_SIGINFO;
    
    	// Define sigEvent
    	this->signalEvent.sigev_value.sival_ptr = this;
    This solved the problem mentioned in my previous post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threads and Timers
    By scioner in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 07:56 AM
  2. Implementing POSIX Timers on AIX - Holy Moly
    By dedham_ma_man in forum C Programming
    Replies: 2
    Last Post: 10-03-2007, 02:36 AM
  3. POSIX on windows anyone?
    By Lynux-Penguin in forum Linux Programming
    Replies: 1
    Last Post: 08-27-2003, 12:56 AM
  4. Timers, Timers, Timers!
    By Stan100 in forum Game Programming
    Replies: 9
    Last Post: 01-24-2003, 03:45 PM
  5. Timers
    By Mox in forum Windows Programming
    Replies: 2
    Last Post: 11-09-2001, 03:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21