【发布时间】:2013-07-19 12:49:14
【问题描述】:
我正在使用 server.socket 将数据流式传输到多个客户端,server.socket 为每个客户端连接使用线程。 我目前有这样的事情:
(def clients (atom ())) ; connected clients defined globally for that namespace
(swap! clients conj a) ; adds a client (which is an atom itself as well), this is in a function that is run on the client's thread
;I want to better the process of removing a client!
(dosync (reset! clients (remove #{a} @clients))) ; removes client from list, run in a function on the client's thread
我运行一个通过每个客户端运行并获取内容的函数,它在多个客户端线程中的每一个上处于无限循环中,因此它同时运行:
(doseq [c @clients]
(print ((deref c) :content))
(flush))
我得出的结论是,在线程中使用 Atom 确实使程序运行顺畅并允许非阻塞读取,所以我对此很满意,除了我觉得重置全局客户端的 Atom 只是为了删除一个列表中的单个客户是一个糟糕的举动。有没有更合适的方法来使用交换来完成这个! ?我为客户端原子选择了列表,因为我在每个连接的客户端上运行 doseq 以获取内容并将其刷新到输出流套接字。
【问题讨论】:
标签: multithreading clojure atomic