On Mon, 26 Nov 2012 19:54:47 GMT, Sönke Ludwig wrote:
I'm not sure what libevent exactly does internally, but the cause turns out to be the blocking
stdin.readln()
call. The following version of the client correctly sends the two strings one by one. However, replacing thesleep()
withforeach(i; 0 .. 1_000_000_000){}
will case both strings to be sent at once after a delay. Looks like an asynchronous way to access stdin needs to be added to vibe....
If you need to read from stdin, you could do something like this for now and read in a separate thread:
...
Not pretty because of the spinning wait, but should work.
It seems that the problem is not in reading the stdin per se... Look at the following code:
import vibe.vibe;
import std.stdio;
import std.concurrency;
void stringReader(Tid parent)
{
string line;
while (line != "exit")
{
line = stdin.readln();
parent.send(line);
}
}
void main()
{
auto conn = connectTcp("127.0.0.1", 1234);
auto reader = spawn(&stringReader, thisTid);
string message;
while (1)
{
message = receiveOnly!string();
if (message == "exit") break;
conn.write(message, true);
//sleep(dur!"secs"(1)); <-- o_O
}
}
Regardless of readln being in separate thread, this code does not work without the sleep being uncommented.
Probably the `core.sync.Condition.wait()
in
receiveOnly()
that uses
pthread_cond_timedwait
blocks the sending, while the nanosleep in
sleep()
` does not.
While reading in a separate thread would be a non-issue for me, this seems like a much bigger problem.
Any ideas?
Thanks,
Martin