News for hackers. Stuff that matters.
Re:Why threads vs events is a nonsensical question. by fauigerzigerk on Sunday August 21, 2011 @03:45AM (#2908735)
In my view, the biggest drawback of the evented approach is that logically sychronous code involving IO becomes very low level spaghetti code. E.g:

  log(write(process(read(source))))
becomes:

  read(source, function(input) {
    write(process(input), function(success) {
      log(success)
    })
  })
Re:Why threads vs events is a nonsensical question. by FooBarWidget on Sunday August 21, 2011 @04:03AM (#2908751)
I don't see that as spaghetti code at all, especially when you want to handle other things while the read() or write() is blocked. Callback-style code allows you to see the interleaving of events. If you use threads then the interleaving is non-obvious.
Re:Why threads vs events is a nonsensical question. by fauigerzigerk on Sunday August 21, 2011 @04:10AM (#2908764)
I guess if you don't see the interleaving in log(write(process(read(source)))) our mental models are too different to merit further discussion of this aspect.

And of course thread based code can do other things as well during blocking IO - on other threads.

Re:Why threads vs events is a nonsensical question. by fauigerzigerk on Sunday August 21, 2011 @11:37AM (#2909388)
I meant to say "logically sequential" not "logically synchronous".
Re:Why threads vs events is a nonsensical question. by dramaticus3 on Sunday August 21, 2011 @07:11AM (#2908976)
use channels, you can make them block so the first loop will wait for logger to finish the logging if you like

    do { 
       source = <- sources // read a source from the sources channel
       if source
           data = read(source)
           process <- data  // send it to the process channel
       else
           process <- nil
    } while source

    fn processor {
        do {
            data = <- process
            result = process(data)
            results <- result
        } while data
    }

    fn writer {
        do {
           data = <- results
           msg = write(data)
           log <- msg
        } while data
    }

    fn logger {
        do {
           msg = <- log
           if msg
                print msg
        } while msg
    }
Re:Why threads vs events is a nonsensical question. by fauigerzigerk on Sunday August 21, 2011 @08:59AM (#2909104)
My ideal version would be something like

  parallel for(item in source)
    log(write(process(read(item))))
And let the compiler/runtime figure out how to do that. It could generate something similar to your code for instance.
Re:Why threads vs events is a nonsensical question. by dramaticus3 on Sunday August 21, 2011 @10:56AM (#2909300)
ok, but now I have a generic logger that I can send all kind of messages to. I can use the source channel anywhere and pop a new data source into it etc. etc.

channels rule :)