vibe.d beta banner
get vibe.d
0.7.30

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(FT, ARGS...)(
  FT func,
  auto ref ARGS args
)
if (is(typeof(*func) == function));

Task runWorkerTaskH(alias method, T, ARGS...)(
  shared(T) object,
  auto ref ARGS args
)
if (is(typeof(__traits(getMember, object, __traits(identifier, method)))));

Example

Running a worker task using a function

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

static void test()
{
	runWorkerTask(&workerFunc, 42);
	runWorkerTask(&workerFunc, cast(ubyte)42); // implicit conversion #719
	runWorkerTaskDist(&workerFunc, 42);
	runWorkerTaskDist(&workerFunc, cast(ubyte)42); // implicit conversion #719
}

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);
	runWorkerTask!(Test.workerMethod)(cls, cast(ubyte)42); // #719
	runWorkerTaskDist!(Test.workerMethod)(cls, 42);
	runWorkerTaskDist!(Test.workerMethod)(cls, cast(ubyte)42); // #719
}

Example

Running a worker task using a function and communicating with it

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

}

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

static void work719(int) {}
static void test719() { runWorkerTaskH(&work719, cast(ubyte)42); }

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 (receiveOnlyCompat!string() == "ping" && --counter) {
			logInfo("pong");
			caller.sendCompat("pong");
		}
		caller.sendCompat("goodbye");
	}
}

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

static class Class719 {
	void work(int) shared {}
}
static void test719() {
	auto cls = new shared Class719;
	runWorkerTaskH!(Class719.work)(cls, cast(ubyte)42);
}
Authors

Sönke Ludwig

Copyright

© 2012-2016 RejectedSoftware e.K.

License

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