Socket server + thread: cpu usage

Tim via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 29 10:44:19 PDT 2014


On Tuesday, 29 April 2014 at 17:35:08 UTC, Tim wrote:
> On Tuesday, 29 April 2014 at 17:19:41 UTC, Adam D. Ruppe wrote:
>> On Tuesday, 29 April 2014 at 17:16:33 UTC, Tim wrote:
>>> Is there anything I'm doing wrong?
>>
>> You should be using a blocking socket. With them, the 
>> operating system will put your thread on hold until a new 
>> connection comes in. Without them, it will endlessly loop 
>> doing absolutely nothing except checking if a new connection 
>> is there yet. Horribly, horribly inefficient.
>
> Alright, this would solve the server-cpu-usage-problem. But 
> what about incoming connections? When I create a new thread and 
> use non-blocking socket I've exactly the same problem. I can 
> also solve this problem by using blocking sockets, but what 
> happens if I do the following:
>
> while(oMyBlockingSocket.isAlive)
> {
>     oMyBlockingSocket.receive(...);
> }
>
> ... and close the connection on the client side? I would never 
> receive anything and receive() waits never comes back or would 
> this throw an exception or similar?

Sorry... I totally forgot... I can use SocketSet's as I already 
asked: 
http://forum.dlang.org/thread/ogghezngzrvvoqaoddbh@forum.dlang.org#post-kivp3e:24jif:241:40digitalmars.com

... but I thought it's also possible using non blocking sockets 
by using Thread.yield().


More information about the Digitalmars-d-learn mailing list