www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Sending a socket to another thread

reply lurker <lurker mailinator.com> writes:
I'd like to modify the listener sample to handle requests in separate
threads but I'm experiencing weird crashes.

Once a connection is established can I send() the relevant socket to
another thread and receive() from there?

Thanks
Feb 15 2011
next sibling parent reply Andrew Wiley <debio264 gmail.com> writes:
On Tue, Feb 15, 2011 at 12:00 PM, lurker <lurker mailinator.com> wrote:
 I'd like to modify the listener sample to handle requests in separate
 threads but I'm experiencing weird crashes.

What platform and version of DMD? There was a bug in the Socket implementation on Windows recently where the WinSock data was either initialized or deinitialized multiple times. I don't remember the details, but I remember having "weird crashes."
 Once a connection is established can I send() the relevant socket to
 another thread and receive() from there?

Yes, I've done this in the past.
Feb 15 2011
parent lurker <lurker mailinator.com> writes:
 What platform and version of DMD? There was a bug in the Socket
 implementation on Windows recently where the WinSock data was

 initialized or deinitialized multiple times. I don't remember the
 details, but I remember having "weird crashes."

Yes, Windows + dmd 2.51.
 Yes, I've done this in the past.

Great. At least is posible. Do you have an example available somewhere? It would really help Thanks
Feb 15 2011
prev sibling next sibling parent Andrew Wiley <debio264 gmail.com> writes:
--00151747b39c4ab30b049c5a1fe8
Content-Type: text/plain; charset=ISO-8859-1

On Tue, Feb 15, 2011 at 4:13 PM, lurker <lurker mailinator.com> wrote:
 What platform and version of DMD? There was a bug in the Socket
 implementation on Windows recently where the WinSock data was

 initialized or deinitialized multiple times. I don't remember the
 details, but I remember having "weird crashes."

Yes, Windows + dmd 2.51.

I looked it up, and that bug was fixed in 2.50, so that's not the problem.
 Yes, I've done this in the past.

Great. At least is posible. Do you have an example available somewhere? It would really help

Actually, I lied, I was getting sockets and spawning new threads with them as arguments. However, the attached source compiles and runs on Linux (although it doesn't do any sort of cleanup when it exits, so I can get errors on the bind call that the address is in use). Disclaimer: I'm not sure whether casting to and away from shared like I do is actually safe, so hopefully someone more knowledgeable can chime in on that. From a type standpoint, as long as ownership of the socket is passed between threads cleanly and only one thread can access the socket at a time, nothing odd should happen, but I'm not sure how/if TLS and the actual details of shared could break this. I'll see what I can dig out as far as that goes. --00151747b39c4ab30b049c5a1fe8 Content-Type: text/x-dsrc; charset=US-ASCII; name="dsocktest.d" Content-Disposition: attachment; filename="dsocktest.d" Content-Transfer-Encoding: base64 X-Attachment-Id: f_gk7exzhb0 bW9kdWxlIGRzb2NrdGVzdDsNCmltcG9ydCBzdGQuc3RkaW87DQppbXBvcnQgc3RkLnNvY2tldDsN CmltcG9ydCBzdGQuY29uY3VycmVuY3k7DQoNCnZvaWQgbWFpbigpIHsNCglhdXRvIHNvY2sgPSBu ZXcgVGNwU29ja2V0KCk7DQoJc29jay5iaW5kKG5ldyBJbnRlcm5ldEFkZHJlc3MoNTEyMikpOw0K CXNwYXduKCZsaXN0ZW4sIGNhc3Qoc2hhcmVkKXNvY2ssIHRoaXNUaWQpOw0KCXdoaWxlKHRydWUp IHsNCgkJcmVjZWl2ZSgoc2hhcmVkKFNvY2tldCkgcykgew0KCQkJYXV0byBzb2NrID0gY2FzdChT b2NrZXQpczsNCgkJCXdyaXRlbG4oImdvdCBzb2NrZXQhIik7DQoJCQlzb2NrLnNlbmQoIllvdSBX aW4hXG4iKTsNCgkJCXNvY2suY2xvc2UoKTsNCgkJfSk7DQoJfQ0KfQ0KDQp2b2lkIGxpc3RlbihU Y3BTb2NrZXQgcywgVGlkIGRlc3QpIHsNCglzLmxpc3Rlbig1KTsNCgl3aGlsZSh0cnVlKSB7DQoJ CWF1dG8gc29jayA9IGNhc3Qoc2hhcmVkKXMuYWNjZXB0Ow0KCQl3cml0ZWxuKCJnb3QgY29ubmVj dGlvbiEiKTsNCgkJc2VuZChkZXN0LCBzb2NrKTsNCgl9DQp9DQo= --00151747b39c4ab30b049c5a1fe8--
Feb 15 2011
prev sibling parent Andrew Wiley <debio264 gmail.com> writes:
On Tue, Feb 15, 2011 at 4:59 PM, Andrew Wiley <debio264 gmail.com> wrote:
 On Tue, Feb 15, 2011 at 4:13 PM, lurker <lurker mailinator.com> wrote:
 What platform and version of DMD? There was a bug in the Socket
 implementation on Windows recently where the WinSock data was

 initialized or deinitialized multiple times. I don't remember the
 details, but I remember having "weird crashes."

Yes, Windows + dmd 2.51.

I looked it up, and that bug was fixed in 2.50, so that's not the problem.
 Yes, I've done this in the past.

Great. At least is posible. Do you have an example available somewhere? It would really help

Actually, I lied, I was getting sockets and spawning new threads with them as arguments. However, the attached source compiles and runs on Linux (although it doesn't do any sort of cleanup when it exits, so I can get errors on the bind call that the address is in use). Disclaimer: I'm not sure whether casting to and away from shared like I do is actually safe, so hopefully someone more knowledgeable can chime in on that. From a type standpoint, as long as ownership of the socket is passed between threads cleanly and only one thread can access the socket at a time, nothing odd should happen, but I'm not sure how/if TLS and the actual details of shared could break this. I'll see what I can dig out as far as that goes.

TDPL seems to say that casting away shared is alright - just make sure it isn't ever actually shared between threads and you should be fine.
Feb 15 2011