That crashed my app as soon as the first client connected.

Even if it hadn't, I'd actually want to have explicit message passing between threads. At least, I think I do. This if for a publish/subscribe system and I'm trying to optimise it further. New subscription requests aren't that common, but publishing is, and that's where most of the time is spent. I naively tried looping over the subscriptions using taskPool.parallel to see which ones to publish to and the app never went anywhere because I think all the threads were trying to write to the same socket at once. I'm not sure.

So my new idea is to keep the connections in separate threads, and when one of the connections issues a publish request, I'd send a message to all the worker threads. Then they'd be responsible for searching only their own connections to see which ones to publish to. So I'd need a way to get their Tids to do that.

On Mon, 18 Nov 2013 16:55:07 GMT, Sönke Ludwig wrote:

On Mon, 18 Nov 2013 15:17:07 GMT, Atila Neves wrote:

listenTCP and listenTCP_s take a delegate/function and runs it in its own fiber. Is there any way to pass this fiber to another thread?

What I want to do is divide the connections up between threads in a thread pool, each thread handling only its own fibers and corresponding sockets.

Is there a way to do that in vibe.d? Thanks in advance,

Atila

You can achieve that using TCPListenOptions.distribute:

void handleConnection(TCPConnection conn)
{
	// ...
}

void startup()
{
	enableWorkerThreads();
	listenTCP(1234, &handleConnection, TCPListenOptions.distribute);
}

This will listen in each of vibe.d's worker threads (equal to the number of CPU cores) on the same socket and let the OS decide which thread handles each incoming connection. This has the advantage of not requiring any inter-thread communication (such as sending the connection using message passing).