Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

passing a parm to a function/method/delegate

Moderators: larsivi kris

Posted: 03/30/09 09:53:15

Hi all, I'm taking my first D/Tango steps, unfortunatly I have two left feet. Thing are not going so well as I hoped. I do have problems with the OO lingo in particular. In the code below I want to use SERVERDOC inside the run method/delegate/function but I can not for my life figure out how I should pass it to run. (The code is an abbrevated extract pointing out my problem only.)

Any help highly appreciated :|

void main(){
	auto serverxml  = cast(char[])File.get("serverparms.xml");
	auto SERVERDOC = new Document!(char);
        void run(){
			auto dwxml = readDwXml ("lasse", SERVERDOC);

        // start server in a separate thread, and wait for it to start
        (new Thread (&run)).start();
Author Message

Posted: 03/30/09 17:12:06

You have a couple of options, but the first questioned that should be answered is if you actually want to share that document between several threads, since once you start doing that you increase the complexity of your application a lot (having to synchronize reads and writes). If you only read the document, then it is simpler though.

Typically, when using threads, you create a sub-class for your thread where you can store such things. Then you could pass the document as a parameter to the constructor for instance.

As for your actual question, Thread's constructor signature tells you if the delegate passed to it can take any parameters at all (and it doesn't). If hypothetically the delegate took a param, then it would be up to the caller to pass it. In the case of Thread and run, it isn't called when you pass the delegate to the Thread constructor, but rather when the thread is started, at which point it is called inside Thread.