On Sat, May 3, 2014 at 6:07 AM, Danesh Daroui danesh.daroui@gmail.com wrote:
Thanks for the info. I thought Pike would run all threads simultaneously when number of threads does not exceed number of available processors and otherwise using time sharing like other languages. I am still learning Pike and hence not familiar with I/O operations in Pike so maybe later will find out when to get benefit of concurrency when Pike is used.
Here's an example:
void client(Stdio.FILE sock) { sock->write("Enter a number: "); int n=(int)sock->gets(); array(string) strs=allocate(n); for (int i=0;i<n;++i) { sock->write("Enter string #%d: ",i); strs[i]=sock->gets(); } sock->write("Bye!\n"); sock->close(); }
void main() { Stdio.Port mainsock=Stdio.Port(12345); while (object sock=mainsock->accept()) { object buffered=Stdio.FILE(); buffered->assign(sock); Thread.Thread(client,buffered); } }
Each client that connects (TELNET makes a viable client here) gets its own thread, and they effectively do run simultaneously. The threads spend most of their time waiting inside gets(), so the internal locking isn't a problem. As far as you're concerned, each one is running straight through its instance of client(), with its own local variables, without interfering with anything else.
To actually make use of multiple cores, though, there are some tricks available. Easiest one is to start separate processes, rather than threads. I haven't often needed to do this, but it's certainly possible.
ChrisA