www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array - Sockets - Clients

reply "Irving Montalvo" <irving moix.in> writes:
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
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
parent reply "Irving Montalvo" <irving moix.in> writes:
How do I pass a socket to concurrency?

send(tid, cli); // Socket cli
----------------------
receive (
(Socket cli) {
   cli.send("1234567890");
}
)
-----
Help me :)
Dec 13 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
parent "Irving Montalvo" <irving moix.in> writes:
Thanks!
-------------
receive(
     (shared(Socket) c) {
       Socket cli = cast(Socket) c;
       while (cli.isAlive()) {
         ubyte[8192] buf;
         ulong bufLen = cli.receive(buf);
         ...
       }
     }
   );
Dec 13 2013