digitalmars.D.learn - Array - Sockets - Clients
- Irving Montalvo (14/14) Dec 13 2013 Socket[ulong] clients;
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (8/21) Dec 13 2013 Data is thread-local by default. clients are not the same array as seen
- Irving Montalvo (10/10) Dec 13 2013 How do I pass a socket to concurrency?
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (33/43) Dec 13 2013 Unfortunately, some cast(shared) is needed at this time:
- Irving Montalvo (12/12) Dec 13 2013 Thanks!
Socket[ulong] clients;
-----------------------------------main()
ulong index = clients.length;
clients[index] = cli; //<-- Socket cli
send(tid, index)
---------------------------------spawned()
receive(
(ulong i) {
clients[i].send('Hello concurrent'); // Error clients.length is
zero
}
);
---------------------------------
Help me please!
Dec 13 2013
On 12/13/2013 03:53 PM, Irving Montalvo wrote:
Socket[ulong] clients;
-----------------------------------main()
ulong index = clients.length;
clients[index] = cli; //<-- Socket cli
send(tid, index)
---------------------------------spawned()
receive(
(ulong i) {
clients[i].send('Hello concurrent'); // Error clients.length is zero
}
);
---------------------------------
Help me please!
Data is thread-local by default. clients are not the same array as seen
by the two threads.
Declare it as 'shared' and pray that you will not have bugs in your
"data-sharing concurrency" app. :)
shared Socket[ulong] clients;
At least I think that's what is happening there. :)
Ali
Dec 13 2013
How do I pass a socket to concurrency?
send(tid, cli); // Socket cli
----------------------
receive (
(Socket cli) {
cli.send("1234567890");
}
)
-----
Help me :)
Dec 13 2013
On 12/13/2013 04:11 PM, Irving Montalvo wrote:
How do I pass a socket to concurrency?
send(tid, cli); // Socket cli
----------------------
receive (
(Socket cli) {
cli.send("1234567890");
}
)
-----
Help me :)
Unfortunately, some cast(shared) is needed at this time:
import std.stdio;
import std.socket;
import std.concurrency;
import core.thread;
struct Done
{}
void main()
{
shared(Socket)[ulong] clients;
clients[10000] =
cast(shared)new Socket(AddressFamily.UNIX, SocketType.STREAM);
auto tid = spawn(&server);
send(tid, clients[10000]);
send(tid, Done());
thread_joinAll();
}
void server()
{
bool done = false;
while (!done) {
receive(
(shared(Socket) client) {
writeln("received client");
},
(Done _) {
done = true;
}
);
}
}
Ali
Dec 13 2013
Thanks!
-------------
receive(
(shared(Socket) c) {
Socket cli = cast(Socket) c;
while (cli.isAlive()) {
ubyte[8192] buf;
ulong bufLen = cli.receive(buf);
...
}
}
);
Dec 13 2013








"Irving Montalvo" <irving moix.in>