Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
December 13, 2013 Array - Sockets - Clients | ||||
---|---|---|---|---|
| ||||
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! |
December 13, 2013 Re: Array - Sockets - Clients | ||||
---|---|---|---|---|
| ||||
Posted in reply to Irving Montalvo | 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
|
December 14, 2013 Re: Array - Sockets - Clients | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | How do I pass a socket to concurrency? send(tid, cli); // Socket cli ---------------------- receive ( (Socket cli) { cli.send("1234567890"); } ) ----- Help me :) |
December 14, 2013 Re: Array - Sockets - Clients | ||||
---|---|---|---|---|
| ||||
Posted in reply to Irving Montalvo | 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
|
December 14, 2013 Re: Array - Sockets - Clients | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | Thanks! ------------- receive( (shared(Socket) c) { Socket cli = cast(Socket) c; while (cli.isAlive()) { ubyte[8192] buf; ulong bufLen = cli.receive(buf); ... } } ); |
Copyright © 1999-2021 by the D Language Foundation