Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
December 31, 2015 socketpair | ||||
---|---|---|---|---|
| ||||
std.socket supports the socketpair() function, but it seems like this api is not really usable in any of the concurrency primitives supported by D. So what is the purpose of the socketpair() support? Basically I am trying to create two threads and am trying to use socketpair() to create two sockets and use them to communicate between the threads, but none of the socket API's allow use of shared socket-pair (probably correctly so). So how else can one implement a foreground event loop that supports both key-presses and network socket events -- I was hoping to do the keyboard event in one thread and use the socketpair to communicate to the actual foreground event loop via the socketpair sockets. Any help on how one would code this in D is welcome. |
December 31, 2015 Re: socketpair | ||||
---|---|---|---|---|
| ||||
Posted in reply to sanjayss | On 31/12/15 3:22 PM, sanjayss wrote: > std.socket supports the socketpair() function, but it seems like this > api is not really usable in any of the concurrency primitives supported > by D. So what is the purpose of the socketpair() support? > > Basically I am trying to create two threads and am trying to use > socketpair() to create two sockets and use them to communicate between > the threads, but none of the socket API's allow use of shared > socket-pair (probably correctly so). So how else can one implement a > foreground event loop that supports both key-presses and network socket > events -- I was hoping to do the keyboard event in one thread and use > the socketpair to communicate to the actual foreground event loop via > the socketpair sockets. > > Any help on how one would code this in D is welcome. Wrong tool for the job. You want message passing not sockets to communicate between threads in this case. You're wanting the functions receive and send relating to: http://dlang.org/phobos/std_concurrency.html |
December 31, 2015 Re: socketpair | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rikki Cattermole | On Thursday, 31 December 2015 at 02:26:23 UTC, Rikki Cattermole wrote: > On 31/12/15 3:22 PM, sanjayss wrote: >> std.socket supports the socketpair() function, but it seems like this >> api is not really usable in any of the concurrency primitives supported >> by D. So what is the purpose of the socketpair() support? >> >> Basically I am trying to create two threads and am trying to use >> socketpair() to create two sockets and use them to communicate between >> the threads, but none of the socket API's allow use of shared >> socket-pair (probably correctly so). So how else can one implement a >> foreground event loop that supports both key-presses and network socket >> events -- I was hoping to do the keyboard event in one thread and use >> the socketpair to communicate to the actual foreground event loop via >> the socketpair sockets. >> >> Any help on how one would code this in D is welcome. > > Wrong tool for the job. > You want message passing not sockets to communicate between threads in this case. > > You're wanting the functions receive and send relating to: > http://dlang.org/phobos/std_concurrency.html Sure -- I understand the concurrency primitives, but what I was trying to achieve was to have a single foreground loop that could select on a network socket while also watching for a keypress (and in C socketpair is one way you would achieve this using one thread to listen for keypresses -- see http://stackoverflow.com/questions/11461106/socketpair-in-c-unix). How do you suggest I achieve this in D? |
December 31, 2015 Re: socketpair | ||||
---|---|---|---|---|
| ||||
Posted in reply to sanjayss | On Thursday, 31 December 2015 at 02:31:09 UTC, sanjayss wrote:
> On Thursday, 31 December 2015 at 02:26:23 UTC, Rikki Cattermole wrote:
>> On 31/12/15 3:22 PM, sanjayss wrote:
>>> [...]
>>
>> Wrong tool for the job.
>> You want message passing not sockets to communicate between threads in this case.
>>
>> You're wanting the functions receive and send relating to:
>> http://dlang.org/phobos/std_concurrency.html
>
> Sure -- I understand the concurrency primitives, but what I was trying to achieve was to have a single foreground loop that could select on a network socket while also watching for a keypress (and in C socketpair is one way you would achieve this using one thread to listen for keypresses -- see http://stackoverflow.com/questions/11461106/socketpair-in-c-unix). How do you suggest I achieve this in D?
OK; one way I realized was to put the network socket select in one thread and the watching for keypress in another thread and then use the concurrency primitives to message pass events to the main thread -- may be a little expensive, but it probably will achieve the effect I desire.
|
December 31, 2015 Re: socketpair | ||||
---|---|---|---|---|
| ||||
Posted in reply to sanjayss | On Thursday, 31 December 2015 at 02:37:07 UTC, sanjayss wrote: > OK; one way I realized was to put the network socket select in one thread and the watching for keypress in another thread and then use the concurrency primitives to message pass events to the main thread -- may be a little expensive, but it probably will achieve the effect I desire. Here is my implementation of this idea: https://github.com/CyberShadow/ae/blob/master/net/sync.d |
January 01, 2016 Re: socketpair | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | On Thursday, 31 December 2015 at 07:48:03 UTC, Vladimir Panteleev wrote:
> On Thursday, 31 December 2015 at 02:37:07 UTC, sanjayss wrote:
>> OK; one way I realized was to put the network socket select in one thread and the watching for keypress in another thread and then use the concurrency primitives to message pass events to the main thread -- may be a little expensive, but it probably will achieve the effect I desire.
>
> Here is my implementation of this idea:
>
> https://github.com/CyberShadow/ae/blob/master/net/sync.d
Thanks, Vladimir...though I didn't fully understand your code example, it gave me the idea to use delegates to capture one of the socketpair sockets for use in a thread where I could write to it and read the other socket to read from in the main task. Just beginning to use D and am getting used to the various features...
|
Copyright © 1999-2021 by the D Language Foundation