Arc Forumnew | comments | leaders | submitlogin
Asynchronus I/O in arc
2 points by zhtw 6052 days ago | 9 comments
I couldn't find any support for asynchronous I/O in arc. Is that so? Am I always supposed to use threads?


4 points by almkglor 6052 days ago | link

This appears to be correct. However, what asynchronous I/O do you need?

It may be possible to write some sort of semaphore-like object which would synchronize across threads, alternatively I think you can probably query the state of threads.

Also, how do you want async I/O? Do you want a callback style where a callback function is called once the I/O completes or fails? Or an async I/O which aborts if it doesn't complete within the necessary time?

Either of the above solutions is implementable using threads.

-----

1 point by zhtw 6051 days ago | link

Actually I can live without asynchronous I/O (smth like posix aio_* functions). Just nonblocking read and select/poll would be enough. I have some experience with threads and believe that they just are not worth the throuble in most cases. I find the twisted python's approach (with reactor and deferreds) much more elegant. So I wanted to implement smth similar in arc but it seems I can't at least in the obvious way.

-----

2 points by almkglor 6051 days ago | link

Well, threads are really pretty easy in Arc.

For example I think this ought to work:

  (def nonblocking-read (port)
    (with (done nil
           rcvd nil
           rv (table))
      (thread (= rcvd (read port))
              (= done t))
      (= rv!isdone (fn () done))
      (= rv!received (fn () rcvd))))

  (= foo (nonblocking-read port))
  (while (~foo!isdone)
    (do-what-you-will))
  (now-process (foo!received))
Not sure though; it really depends on how well mzscheme handles blocking I/O.

-----

1 point by zhtw 6051 days ago | link

Yes, thanks, it must work. But it means that you need to have exactly N suspended threads when you're waiting for data from N sockets. Thread pool helps a lot but in worst case you will still need exactly N threads.

-----

2 points by almkglor 6050 days ago | link

Thread management is done automatically for you by the underlying mzscheme. A bit more of research seems to suggest that mzscheme can actually handle blocking I/O even if it uses green threads, and it can also have OS threads if compiled with support for it.

From what little I could grok from http://download.plt-scheme.org/doc/insidemz/insidemz-Z-H-8.h... it seems that there's not much overhead per thread anyway.

What are your concerns?

-----

3 points by soegaard 6050 days ago | link

MzScheme does not use OS threads. A long time ago it did, but it is pretty hard to make it work on Windows, OS X and Linux at the same time. Also context switching becomes cheaper without OS threads.

If you have found anything on OS threads in "Inside MzScheme" it must be in the section on how to embed MzScheme into a C program (with its own thread).

-----

1 point by zhtw 6050 days ago | link

Oh, is that rather usual that scheme implementation uses green threads? If so, maybe I really shouldn't worry about it. I was concerned only because I thought it was expensive to have one system thread per connection.

Thanks for the link.

-----

1 point by soegaard 6050 days ago | link

Most Scheme implementations doesn't use OS threads due to the cost of context switches. It is standard practice to use one thread for each connection.

See for example this very well written introduction to Systems Programming with PLT Scheme:

http://pre.plt-scheme.org/docs/html/more/index.html

-----

1 point by almkglor 6050 days ago | link

Maybe, I'm not sure. I couldn't grok the doc much. AFAIK mzscheme uses green threads only if it isn't compiled with "better" thread support.

As for cost per thread - no, I don't worry about it, it seems that mzscheme threads are reasonably light.

-----