Function runWorkerTaskH

Runs a new asynchronous task in a worker thread, returning the task handle.

This function will yield and wait for the new task to be created and started in the worker thread, then resume and return it.

Only function pointers with weakly isolated arguments are allowed to be able to guarantee thread-safety.

Prototypes

Task runWorkerTaskH(R, ARGS...)(
  R function(ARGS) func,
  ARGS args
);

Task runWorkerTaskH(alias method, T, ARGS...)(
  shared(T) object,
  ARGS args
);

Example

Running a worker task using a function

static void workerFunc(int param)
{
	logInfo("Param: %s", param);
}

static void test()
{
	runWorkerTask(&workerFunc, 42);
}

Example

Running a worker task using a class method

static class Test {
	void workerMethod(int param)
	shared {
		logInfo("Param: %s", param);
	}
}

static void test()
{
	auto cls = new shared Test;
	runWorkerTask!(Test.workerMethod)(cls, 42);
}

Example

Running a worker task using a function and communicating with it

static void workerFunc(Task caller)
{
	int counter = 10;
	while (receiveOnly!string() == "ping" && --counter) {
		logInfo("pong");
		caller.send("pong");
	}
	caller.send("goodbye");

}

static void test()
{
	Task callee = runWorkerTaskH(&workerFunc, Task.getThis);
	do {
		logInfo("ping");
		callee.send("ping");
	} while (receiveOnly!string() == "pong");
}

Example

Running a worker task using a class method and communicating with it

static class Test {
	void workerMethod(Task caller) shared {
		int counter = 10;
		while (receiveOnly!string() == "ping" && --counter) {
			logInfo("pong");
			caller.send("pong");
		}
		caller.send("goodbye");
	}
}

static void test()
{
	auto cls = new shared Test;
	Task callee = runWorkerTaskH!(Test.workerMethod)(cls, Task.getThis());
	do {
		logInfo("ping");
		callee.send("ping");
	} while (receiveOnly!string() == "pong");
}

Authors

Sönke Ludwig

Copyright

© 2012-2014 RejectedSoftware e.K.

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.