<div class="gmail_quote">On 8 December 2011 16:27, Jens Mueller <span dir="ltr"><<a href="mailto:jens.k.mueller@gmx.de">jens.k.mueller@gmx.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="HOEnZb"><div class="h5">Manu wrote:<br>
> On 8 December 2011 00:20, Manu <<a href="mailto:turkeyman@gmail.com">turkeyman@gmail.com</a>> wrote:<br>
><br>
> > Hey peoples,<br>
> ><br>
> > So this might be a bit off topic... but I'm trying to think about the best<br>
> > way to write a small program in the most D-ish way possible (just to make<br>
> > sure I'm giving myself the most realistic experience with the language),<br>
> > and I wanted to get some thoughts.<br>
> > I don't know which way to go on this. I know what I'd do in various other<br>
> > languages, but I'm curious to hear the communities opinion. I think part of<br>
> > the problem is that D's networking libraries are pretty incomplete... but<br>
> > here it is anyway.<br>
> ><br>
> > I basically just want to write a trivial echo server which may have<br>
> > hundreds of thousands of connections split into small groups, and whenever<br>
> > data is received from a member in any group, it is basically echoed to the<br>
> > others in that group.<br>
> > Sounds super simple...<br>
> ><br>
> > My instinct is to have a listener on the main thread, and spawn a thread<br>
> > per group, each blocking on a select().. I think the questions is around<br>
> > thread safety and group management (managing the connection list for each<br>
> > group), and how to interrupt a blocking select() when a new connection has<br>
> > entered the group...<br>
> > "The D way" to solve these questions is a mystery to me. I just feel like<br>
> > I'm writing C code, manually managing thread safety, timeout logic. I feel<br>
> > like D offers some fundamental features that should make solving this<br>
> > problem a whole lot simpler that I must be completely missing...<br>
> ><br>
> > So, in a few sentences... simple problem, what would you do?<br>
> ><br>
><br>
> Nobody at all? :(<br>
><br>
> Quite seriously. I'm trying to work out how D's threading/thread safety<br>
> architecture can help me here.. Also since there's no collections, I'm<br>
> using the associative arrays. How do I manage thread safety when accessing<br>
> those? Is there a generic lock mechanism that I've missed along the way<br>
> without manually creating semaphores? Networking does seem to be pretty<br>
> bare, I'm basically writing C sockets code.<br>
> One of the results of my experiment here is to identify some critical<br>
> things that appear to be missing from the language. Places libraries should<br>
> really step in and make the task simple. This is a super simple network<br>
> app, but so far, it's just as complicated as if it were written in C.<br>
<br>
</div></div>I'm probably wrong or not getting it.<br>
But to me it seems you have two different things:<br>
First threads and second connections. For the first I would give<br>
std.concurrency a try (message passing between the threads). And<br>
managing a set of connections within a thread is plain C. So using<br>
select as you do is probably good. If you need to adjust the select call<br>
from time to time you should not let select block indefinitely. Use a<br>
timeout. Another option is pselect which has a signal mask. I suppose<br>
this can also be used to signal abort.<br>
<span class="HOEnZb"><font color="#888888"><br>
Jens<br>
</font></span></blockquote></div><br><div>This is basically exactly what I have (although I'm using core.thread, I'll swap it for std.concurrency, it does look nicer). But it just feels... lame<i>.</i></div><div>
I'm not sure why though, I can't quite put my finger on it. Maybe something irks me about using select() this way, it feels, 1980s :P</div><div><br></div><div>The std.concurrency documentation is pretty bad.</div>
<div>So is std.concurrency.spawn() just effectively a helper for spawning a thread that has mailbox support?</div><div>std.concurrency.receive() has no documentation ;) .. Blocking/Non-blocking? Should I use the one with duration==0 to do a poll? Perhaps there should be an explicit poll() method?</div>
<div>What is prioritySend()? (also undocumented)</div>