FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Signal handler and delegates

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
lars_kirchhoff



Joined: 27 May 2008
Posts: 9

PostPosted: Wed Jan 21, 2009 11:28 am    Post subject: Signal handler and delegates Reply with quote

Hello,

I'm trying to write a class that can be used to create daemon programs under linux. The main functionality of the class is to detach the program from console start a given number of parallel processes. Each of the processes should run the same code. I added a signal handler that start a new child process as soon as another child process is terminated. If the parent is killed process is killed all child processes are terminated as well. This works so far with a hard coded function that is within the scope of the class.

Now I want to be able to pass a function/method from another object to this daemon class that is executed in the child processes.

I used a delegate as a in the function that starts the children:

Code:

public static void start(int delegate() dg) {
    ...fork code...
    return dg();
}


which is called with a reference to the object method that should be executed.

Code:

DoSomething f = new DoSomething();
Daemon d = new Daemon();
d.start(&f.cool);


This works fine as long as no signal handler is involved. Within the signal handler the start function is called in order to create a new child if another is terminated. The problem with the signal handler is that it only takes one argument (the signal) and it can only call static functions. This is why start is static and additionally it means I cannot pass the reference.

At the moment I'm lost. I'm new to D. I can think of two different approaches. First, another way to create a new child, which I do not how. Second another approach to call the function or object method.

Any help is appreciated.

thanks lars
Back to top
View user's profile Send private message
lars_kirchhoff



Joined: 27 May 2008
Posts: 9

PostPosted: Thu Jan 22, 2009 12:21 pm    Post subject: Reply with quote

ok, solved it.

I adapted the daemon class to a singleton pattern and moved the signal handler outside the daemon class. The daemon class has a member variable that holds a reference to the delegate function that calls the code that is executed in the childs.

This way the signal handler does not need to know about the delegate, because it just initiates the daemon with singleton pattern, which still holds the function reference in the member variable.

Is there any other possible solution, which might be more D like?

/Lars
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group