3 ms·
I think one of the nicest things about this are its origins in CSP. By using channel communication for synchronization between threads you eliminate all the ha
by jgrahamc 17y ago
I think one of the nicest things about this are its origins in CSP. By using channel communication for synchronization between threads you eliminate all the hassle of worrying about mutexes and shared memory.
And they have a nice choice operator called select which allows the language to read from multiple channels at the same time and make a random choice about competing channels to send to. See this example:
var c, c1, c2 chan int;
var i1, i2 int;
select {
case i1 = <-c1:
print("received ", i1, " from c1\n");
case c2 <- i2:
print("sent ", i2, " to c2\n");
default:
print("no communication\n");
}
for { // send random sequence of bits to c
select {
case c <- 0: // note: no statement, no fallthrough, no folding of cases
case c <- 1:
}
}
The style corresponds to the CSP choice operator where the bottom select is doing something like
P = ( 0 -> P ) OR ( 1 -> P )
OR there is the CSP non-deterministic choice operator. Either branch can go ahead.
CSP doesn't make a distinction between sending and receiving on a channel (it doesn't have channels, merely the parallel, or interface parallel, operator to combine processes). Essentially sending is offering to synchronize on a single value and receiving is accepting to synchronize on any value: run those in parallel you get a communication channel.
- jacquesm 17y agoThere is an OS called QnX that does this by using synchronized (for the most part) interprocess communication using messages. Makes you wonder what you could do if you extended the channel concept to the OS level and to machine-to-machine communication. I guess you'd end up with Plan-9 sooner or later.